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:

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-

The phrase

for line in data:
  ...

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

So if you inserted the statement

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

you would get

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

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-

Here is a way to store your result in a list.

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 :

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

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...

I'd suggest

file_in =  open("data/%s.txt" % data)
file_list = []
for line in file_in:
    item = line.split(",")[5]
    file_list.append(item)

oops, somebody gave the solution before I did :)

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:

my_list = []
for line in my_file:
    my_list.append( line )

Now on ever iteration of the for loop, we'll *append* each line to the list...

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)

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

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

Thanks again...

grant-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.