Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Storing lines from a text file

Reply
Thread Tools

Storing lines from a text file

 
 
bradfordh@gmail.com
Guest
Posts: n/a
 
      01-29-2006
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-

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-29-2006
wrote:

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



 
Reply With Quote
 
 
 
 
bradfordh@gmail.com
Guest
Posts: n/a
 
      01-29-2006
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.

 
Reply With Quote
 
Kirk McDonald
Guest
Posts: n/a
 
      01-29-2006
wrote:
> 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.
>


With a list:
http://python.org/doc/2.4.2/tut/node...00000000000000

-Kirk McDonald
 
Reply With Quote
 
bradfordh@gmail.com
Guest
Posts: n/a
 
      01-29-2006
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?

 
Reply With Quote
 
Kirk McDonald
Guest
Posts: n/a
 
      01-29-2006
wrote:
> 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
 
Reply With Quote
 
bradfordh@gmail.com
Guest
Posts: n/a
 
      01-30-2006
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'

 
Reply With Quote
 
Kirk McDonald
Guest
Posts: n/a
 
      01-30-2006
wrote:
> 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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      01-30-2006
<> wrote:

> 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
 
Reply With Quote
 
bradfordh@gmail.com
Guest
Posts: n/a
 
      01-30-2006
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] "

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Storing lines from a text file bradfordh@gmail.com Python 0 01-29-2006 11:00 PM
reading file and storing information of lines with varying length christrier Perl Misc 12 11-16-2005 08:43 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57