Trouble with readline - HELP

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 5
Reputation: g_e_young is an unknown quantity at this point 
Solved Threads: 0
g_e_young g_e_young is offline Offline
Newbie Poster

Trouble with readline - HELP

 
0
  #1
Mar 1st, 2009
Hi - This is a simple problem, I'm sure, but I'm stumped. I need to read only one column from a multi-column file. The column is always in the same position, so I've decided to simply isolate the location in the row while reading the data. Here is my code:

  1. data = open ("files/" + data + ".txt", 'r')
  2. for line in data:
  3. line = line[34:38]
  4. file_list = line.readline()

But I'm getting an error message which says: "AttributeError: 'Str' has no attribute 'readline'

The data appears as follows:

01/27/2009,0840,80.13,80.17,79.81,79.99,16462,29701
01/27/2009,0845,79.97,79.99,79.37,79.44,65375,55565
01/27/2009,0850,79.48,79.57,79.31,79.42,35650,33070
01/27/2009,0855,79.44,79.89,79.41,79.87,35252,18583
01/27/2009,0900,79.87,79.98,79.74,79.77,33092,39951
01/27/2009,0905,79.75,80.01,79.68,80.01,6386,6000

Thanks so much for your help. Also, if you can suggest a better way just to read one of the columns, that would also be appreciated. I know I'm in trouble if the characters between each comma start to vary. grant-
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Trouble with readline - HELP

 
0
  #2
Mar 1st, 2009
The phrase

  1. for line in data:
  2. ...

assigns the variable "line" to each interated string terminated by '\n' within the data.

So if you inserted the statement

  1. data = open ("files/" + data + ".txt", 'r')
  2. for line in data:
  3. print line
  4. line = line[34:38]
  5. file_list = line.readline()

you would get

  1. 01/27/2009,0840,80.13,80.17,79.81,79.99,16462,29701
  2. AttributeError: 'Str' has no attribute 'readline'

Now the problem becomes clear: in Line 4, you're taking the string you already have, and then trying to readline it from the file again!

Just kill the readline() and print the results of the slice in line 3, and you'll be good.

Jeff
Last edited by jrcagle; Mar 1st, 2009 at 11:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: g_e_young is an unknown quantity at this point 
Solved Threads: 0
g_e_young g_e_young is offline Offline
Newbie Poster

Re: Trouble with readline - HELP

 
0
  #3
Mar 2nd, 2009
OK, progress has been made as there's no longer an error, but file_list ends up with the final value from data, rather than a string of all values in the file (in that character range). When you say "Just kill the readline() and print the results of the slice in line 3" do you mean to simply end the for loop with file_list = line?

Somehow I'm not getting the full concept here. Sorry. The bit about "print the results" is throwing me off since I really want to load all the data in to file_list, not "print" it.

Thanks again for the help. g-
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 149
Reputation: jice is on a distinguished road 
Solved Threads: 38
jice jice is offline Offline
Junior Poster

Re: Trouble with readline - HELP

 
0
  #4
Mar 2nd, 2009
Here is a way to store your result in a list.
  1. data = open ("files/" + data + ".txt", 'r')
  2. file_list=[]
  3. for line in data:
  4. print line
  5. file_list.append(line[34:38])

Just one point :
instead of "line[34:38]" which is dangerous in this kind of csv files, you can use :
  1. file_list.append(line.split(",")[5][0:2])
This splits your line in a list, using "," as the splitting marker, takes the 5th element and takes the 2 first characters.
You can even do
  1. file_list.append(line.split(",")[5].split(".")[0])
But if you have some more complex processes to do, yuou should have a look at the csv module...
Last edited by jice; Mar 2nd, 2009 at 4:15 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 963
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 221
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Trouble with readline - HELP

 
0
  #5
Mar 2nd, 2009
I'd suggest
  1. file_in = open("data/%s.txt" % data)
  2. file_list = []
  3. for line in file_in:
  4. item = line.split(",")[5]
  5. file_list.append(item)
oops, somebody gave the solution before I did
Last edited by Gribouillis; Mar 2nd, 2009 at 4:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Trouble with readline - HELP

 
0
  #6
Mar 2nd, 2009
Originally Posted by g_e_young View Post
file_list ends up with the final value from data, rather than a string of all values in the file
On every iteration you're simply over-writing the value of file_list. The proper way to use a list is:
  1. my_list = []
  2. for line in my_file:
  3. my_list.append( line )
Now on ever iteration of the for loop, we'll *append* each line to the list...
Originally Posted by g_e_young View Post
When you say "Just kill the readline() and print the results of the slice in line 3" do you mean to simply end the for loop with file_list = line?
Yes he is saying to remove the line of code containing readline(); however file_list = line is not going to give you the results that you want (refer to my first section of comments)
Originally Posted by g_e_young View Post
Somehow I'm not getting the full concept here. Sorry. The bit about "print the results" is throwing me off since I really want to load all the data in to file_list, not "print" it.
He was only suggesting that you print the results for your own debug purposes. Using print statements periodically throughout your code helps when you don't know what's happening... it helps to show you what each step is doing to your data.

HTH
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: g_e_young is an unknown quantity at this point 
Solved Threads: 0
g_e_young g_e_young is offline Offline
Newbie Poster

Re: Trouble with readline - HELP

 
0
  #7
Mar 2nd, 2009
Thanks guys - daniweb just rocks. After two days of pulling my hair out, I can finally start regrowing it.

Thanks again...

grant-
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC