By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Reading a specific line in a text file

P: 2

I need a help regarding reading a line in a text file. I have some files with columns but some header information at the top. Some of these header lines begin with a *, and some of them with nothing. I have to read the 9th line and 5th column, then extract the number from here and save it to another text file.

I was thinking of some solutions.
1) I can delete the first 8 lines and read the columns straightforward.
2) I first read the 9th line and then the 5th column, by using f.readline() or etc. However, readline only reads the first line and I could not tell to read the 9th line.

I could not made neither of those solutions. The second would be much better because at the end I do not lose the header info. I have hundreds of these files, any help would be much appreciated.

Apr 28 '09 #1
Share this Question
Share on Google+
3 Replies

P: 144
you could read the entire file in an array and use that array like this
and continue from there on out
Apr 28 '09 #2

Expert Mod 2.5K+
P: 2,851
micmast's suggestion should work by using file method f.readlines(). List index 0 is your header and index 8 is your line of data. Assuming the separator is a comma:
Expand|Select|Wrap|Line Numbers
  1. f = open(file_name)
  2. fileList = f.readlines()
  3. f.close()
  4. fifth_column = fileList[8].split(',')[4]
Apr 28 '09 #3

P: 2
Great, it is working perfectly.

Thanks a lot...
Apr 28 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.