jxn jxn - 11 months ago 39
Linux Question

python writing list to text file result in different lengths

i have 2 lists of strings with the same length but when i write them to a file where each item appear on separate lines in the file, they length of the list and file do not match:

print len(x)
print len(y)


However when i write each item in the list to a text file:
the number of lines in the text file do not match to length of the list.

with open('a.txt', 'wb') as f:
for i in x[:222500]:
print >> f, i

in linux,
wc -l a.txt
gives 222499 which is right.

with open('b.txt', 'wb') as f:
for i in y[:222500]:
print >> f, i

in linux,
wc -l b.txt
gives 239610 which is wrong.

when i
vi b.txt
in the terminal, it did have 239610 lines so i am quite confused as to why this is happening..

How can i debug this?

Answer Source

The only possible way for finding more lines in b.txt than the number of string written is that some of the strings in y actually contain new lines.

Here is a small example

l = [ 'a', 'b\nc']
print len(l)
with open('tst.txt', 'wb') as fd:
    for i in l:
        print >> fd, i

This little code will print 2 because list l contains 2 elements, but the resulting file will contain 3 lines: