[Tutor] Another string question

Kent Johnson kent37 at tds.net
Fri Mar 23 12:33:05 CET 2007


Jay Mutter III wrote:
> Thanks for the response
> Actually the number of lines this returns is the same number of lines 
> given when i put it in a text editor (TextWrangler).
> Luke had mentioned the same thing earlier but when I do change read to 
> readlines  i get the following
> 
> 
> Traceback (most recent call last):
>   File "extract_companies.py", line 17, in ?
>     count = len(text.splitlines())
> AttributeError: 'list' object has no attribute 'splitlines'

I think maybe you are confused about the difference between "all the 
text of a file in a single string" and "all the lines of a file in a 
list of strings."

When you open() a file and read() the contents, you get all the text of 
a file in a single string. len() will give you the length of the string 
(the total file size) and iterating over the string gives you one 
character at at time.

Here is an example of a string:
In [1]: s = 'This is text'
In [2]: len(s)
Out[2]: 12
In [3]: for i in s:
    ...:     print i
    ...:
    ...:
T
h
i
s

i
s

t
e
x
t

On the other hand, if you open() the file and then readlines() from the 
file, the result is a list of strings, each of with is the contents of 
one line of the file, up to and including the newline. len() of the list 
is the number of lines in the list, and iterating the list gives each 
line in turn.

Here is an example of a list of strings:
In [4]: l = [ 'line1', 'line2' ]
In [5]: len(l)
Out[5]: 2
In [6]: for i in l:
    ...:     print i
    ...:
    ...:
line1
line2

Notice that s and l are *used* exactly the same way with len() and for, 
but the results are different.

As a further wrinkle, there are two easy ways to get all the lines in a 
file and they give slightly different results.

open(...).readlines() returns a list of lines in the file and each line 
includes the final newline if it was in the file. (The last line will 
not include a newline if the last line of the file did not.)

open(...).read().splitlines() also gives a list of lines in the file, 
but the newlines are not included.

HTH,
Kent


More information about the Tutor mailing list