Suresh
Suresh

Reputation: 65

Parsing data files for specific columns and specific rows using python

I have a data file that contains data in the following format. I would like to parse the data into 3 different files based on the id number and only get columns 3, 4 5 6 and 9. How do I do it in python!? The data looks like the below example with each column representing the following quantities:

I only would like to retrieve data which has columns 3 4 5 6 and 9 into 3 seperate files based on the same ID (just the numbers which are colored by both yellow and blue in the pic below!)

density ID time x y z u v w dia

enter image description here

Any help will be appreciated!

Upvotes: 0

Views: 1210

Answers (1)

Arthur
Arthur

Reputation: 1984

If every column is separated by a whitespace and you know that there won't be whitespaces in "cells", then you could do something very simple as:

f=open('/path/to/file')
for li in f.readlines():
    data = li.strip().split(' ')
    print data

strip will get rid of the \n at the end of every line ; here I split with ' ' but you could do it with anything, such as a tab \t.

data[i] will get you the value for column i.

To write files, juste use open with the 'w' option and use the write() function.

Upvotes: 1

Related Questions