Skip to content
Advertisement

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)

317858
317858

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?

Advertisement

Answer

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', 'bnc']
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:

a
b
c
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement