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
JavaScript
x
8
1
bit address data code new
2
3
0 0FA34B 0002623748594759834784648294918748734610484 123 00000001
4
5
1 AB102C 2814699837426459814735985739460746706706600 124 00000002
6
7
1 C102BC 1237748798359846709648378598089837658736738 125 00000003
8
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:
JavaScript
1
6
1
0002623748594759834784648294918748734610484 00000001
2
3
2814699837426459814735985739460746706706600 00000002
4
5
1237748798359846709648378598089837658736738 00000003
6
This is my current attempt:
JavaScript
1
5
1
for l in fin:
2
if l.strip("n '"):
3
column = l.split(" ")
4
fout.write("%s %s n" % (column[3:4], column[4:5]))
5
Current output:
JavaScript
1
16
16
1
--name of file
2
3
--data of file
4
5
[] []
6
7
['']
8
9
['']
10
11
['0002623748594759834784648294918748734610484'] ['00000001']
12
13
['2814699837426459814735985739460746706706600'] ['00000002']
14
15
['1237748798359846709648378598089837658736738'] ['00000003']
16
Advertisement
Answer
You can do something like this:
JavaScript
1
4
1
columns = l.split(" ")
2
if len(columns) == 5: # validate line that it has exactly 5 columns
3
fout.write("%s %s n" % (columns[3], columns[4]))
4