Skip to content
Advertisement

How to unlist given list in python?

I have to copy particular columns from a given file to another file. but while copying that columns to another file i don’t need them in list format. How can i do that?

eg suppose file is abc.txt.gz

bit address  data                                        code  new
 
0   0FA34B   0002623748594759834784648294918748734610484 123   00000001

1   AB102C   2814699837426459814735985739460746706706600 124   00000002

1   C102BC   1237748798359846709648378598089837658736738 125   00000003

From this data i needed to copy 3rd and 4th column and move to another file.

Expected output: data.txt

In expected output I only need data & new. I don’t want nothing else to get copied to my new file. Example:

0002623748594759834784648294918748734610484 00000001

2814699837426459814735985739460746706706600 00000002

1237748798359846709648378598089837658736738 00000003

This is my current attempt:

for l in fin:
    if l.strip("n '"):
        column = l.split(" ")
        fout.write("%s %s n" % (column[3:4], column[4:5]))

Current output:

--name of file

--data of file

[] []

['']

['']

['0002623748594759834784648294918748734610484'] ['00000001']

['2814699837426459814735985739460746706706600'] ['00000002']

['1237748798359846709648378598089837658736738'] ['00000003']

Advertisement

Answer

You can do something like this:

       columns = l.split(" ")
       if len(columns) == 5: # validate line that it has exactly 5 columns
           fout.write("%s %s n" % (columns[3], columns[4]))
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement