Storing lines from a text file

B

bradfordh

Hello everyone.

I am not sure how hard of a question is, but I do know that I need some
help if you can give it. What I want to do is read the lines in a text
file and store each line into a variable. I believe that I can use
readlines() to read the individual lines, but how would I store each
line into a variable for further analysis? Thanks for any and all
suggestions.


-Tempo-
 
F

Fredrik Lundh

I am not sure how hard of a question is, but I do know that I need some
help if you can give it. What I want to do is read the lines in a text
file and store each line into a variable. I believe that I can use
readlines() to read the individual lines, but how would I store each
line into a variable for further analysis? Thanks for any and all
suggestions.

any reason you cannot do the analysis on the list you get from readlines ?

if you insist on having the lines in individual variables, you can use straight-
forward sequence unpacking:

a, b, c, d, e = f.readlines()

</F>
 
B

bradfordh

so this:
a, b, c, d, e =f.readlines()

...this will put the first line in a, second in b, etc? How do I
accomplish this when I'm not sure how many lines there are going to be
every time? Thanks.
 
B

bradfordh

With what kind of list? I don't see how I can do it with a list unless
I create one indefinate list and use the objects in the indefinate list
for the names of the lists to hold the lines of text. Is that how you
are suggesting that I do it?
 
K

Kirk McDonald

With what kind of list? I don't see how I can do it with a list unless
I create one indefinate list and use the objects in the indefinate list
for the names of the lists to hold the lines of text. Is that how you
are suggesting that I do it?

You're thinking too hard. Say you want to read in all the lines from the
file object f and just print them out one at a time:

lines = f.getlines()
for line in lines:
print line

Simple.

-Kirk McDonald
 
B

bradfordh

I keep getting an error when I try to use what you said Mr. McDonald. I
think I've got something wrong, but take a look if you can.

log = open('C:\log_0.txt')
lines = log.getlines()
for line in lines:
print line

When I debug it the error I get is the following:
AttributeError: 'file' object has no attribute 'getlines'
 
K

Kirk McDonald

I keep getting an error when I try to use what you said Mr. McDonald. I
think I've got something wrong, but take a look if you can.

log = open('C:\log_0.txt')
lines = log.getlines()
for line in lines:
print line

When I debug it the error I get is the following:
AttributeError: 'file' object has no attribute 'getlines'

D'oh! That's because the method is readlines(). Stupid brain:

log = open('C:\log_0.txt')
lines = log.readlines()
for line in lines:
print line

-Kirk McDonald
 
A

Alex Martelli

I keep getting an error when I try to use what you said Mr. McDonald. I
think I've got something wrong, but take a look if you can.

log = open('C:\log_0.txt')
lines = log.getlines()
for line in lines:
print line

When I debug it the error I get is the following:
AttributeError: 'file' object has no attribute 'getlines'

Yep, the method's name is readlines, not getlines.


Alex
 
B

bradfordh

Alright now that I know how to read the text file line by line, my
question is almost answered completely. And before I ask anything else
I want to say thanks for the help recieved already.

Well the last thing I need help on is storing each line into one big
list. Each line needs to have its own place in the list, not just the
entire file into the first space of the list. For example if the list
is titled s;
then..
s[0] ..would hold the first line of the text file
s[1] ..would hold the second line of the text file
" "
" "
" "
s[n] "
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top