943,691 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 629
  • Python RSS
Mar 1st, 2009
0

Trouble with readline - HELP

Expand Post »
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:

python Syntax (Toggle Plain Text)
  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-
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g_e_young is offline Offline
5 posts
since Aug 2008
Mar 1st, 2009
0

Re: Trouble with readline - HELP

The phrase

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 2nd, 2009
0

Re: Trouble with readline - HELP

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-
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g_e_young is offline Offline
5 posts
since Aug 2008
Mar 2nd, 2009
0

Re: Trouble with readline - HELP

Here is a way to store your result in a list.
python Syntax (Toggle Plain Text)
  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 :
python Syntax (Toggle Plain Text)
  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
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 64
Solved Threads: 56
Posting Whiz in Training
jice is offline Offline
225 posts
since Oct 2007
Mar 2nd, 2009
0

Re: Trouble with readline - HELP

I'd suggest
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 930
Solved Threads: 667
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Mar 2nd, 2009
0

Re: Trouble with readline - HELP

Click to Expand / Collapse  Quote originally posted by g_e_young ...
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:
python Syntax (Toggle Plain Text)
  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...
Click to Expand / Collapse  Quote originally posted by g_e_young ...
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)
Click to Expand / Collapse  Quote originally posted by g_e_young ...
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
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Mar 2nd, 2009
0

Re: Trouble with readline - HELP

Thanks guys - daniweb just rocks. After two days of pulling my hair out, I can finally start regrowing it.

Thanks again...

grant-
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g_e_young is offline Offline
5 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Using regex to wrap text
Next Thread in Python Forum Timeline: [wxWidgets] wx.Toolbar & SetToolBitmapSize





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC