| | |
Trouble with readline - HELP
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
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-
python Syntax (Toggle Plain Text)
data = open ("files/" + data + ".txt", 'r') for line in data: line = line[34:38] 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-
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
The phrase
assigns the variable "line" to each interated string terminated by '\n' within the data.
So if you inserted the statement
you would get
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
Python Syntax (Toggle Plain Text)
for line in data: ...
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)
data = open ("files/" + data + ".txt", 'r') for line in data: print line line = line[34:38] file_list = line.readline()
you would get
Python Syntax (Toggle Plain Text)
01/27/2009,0840,80.13,80.17,79.81,79.99,16462,29701 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.
•
•
Join Date: Aug 2008
Posts: 5
Reputation:
Solved Threads: 0
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-
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-
•
•
Join Date: Oct 2007
Posts: 149
Reputation:
Solved Threads: 38
Here is a way to store your result in a list.
Just one point :
instead of "line[34:38]" which is dangerous in this kind of csv files, you can use :
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
But if you have some more complex processes to do, yuou should have a look at the csv module...
python Syntax (Toggle Plain Text)
data = open ("files/" + data + ".txt", 'r') file_list=[] for line in data: print line 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)
file_list.append(line.split(",")[5][0:2])
You can even do
python Syntax (Toggle Plain Text)
file_list.append(line.split(",")[5].split(".")[0])
Last edited by jice; Mar 2nd, 2009 at 4:15 am.
I'd suggest
oops, somebody gave the solution before I did
python Syntax (Toggle Plain Text)
file_in = open("data/%s.txt" % data) file_list = [] for line in file_in: item = line.split(",")[5] file_list.append(item)
Last edited by Gribouillis; Mar 2nd, 2009 at 4:21 am.
•
•
•
•
file_list ends up with the final value from data, rather than a string of all values in the file
python Syntax (Toggle Plain Text)
my_list = [] for line in my_file: my_list.append( line )
•
•
•
•
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?
file_list = line is not going to give you the results that you want (refer to my first section of comments)•
•
•
•
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.
HTH
![]() |
Similar Threads
- Trouble with select button, please help! (VB.NET)
- Having trouble with my main method (C#)
- Reading external data into array of struct (Visual Basic 4 / 5 / 6)
- having some trouble with my project (Java)
- Round-Robin Scheduling using threads, having some trouble (VB.NET)
- Scanf Equivalent (Java)
- Newbie needs help with Streams pls (Java)
- hey guys; having some trouble with this program i'm writing (Java)
Other Threads in the Python Forum
- Previous Thread: Using regex to wrap text
- Next Thread: [wxWidgets] wx.Toolbar & SetToolBitmapSize
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array backend beginner binary book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary drive dynamic examples excel file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion redirect refresh scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython






