python writing list to text file result in different lengths
<p>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:</p>

<pre><code>print len(x)
print len(y)

317858
317858
</code></pre>

<p>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. </p>

<pre><code>with open('a.txt', 'wb') as f:
    for i in x[:222500]:
        print &gt;&gt; f, i
</code></pre>

<p>in linux, <code>wc -l a.txt</code> gives 222499 which is right. </p>

<pre><code>with open('b.txt', 'wb') as f:
    for i in y[:222500]:
        print &gt;&gt; f, i
</code></pre>

<p>in linux, <code>wc -l b.txt</code> gives 239610 which is wrong. </p>

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

<p>How can i debug this?</p>

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

<p>Here is a small example</p>

<pre><code>l = [ 'a', 'b\nc']
print len(l)
with open('tst.txt', 'wb') as fd:
    for i in l:
        print &gt;&gt; fd, i
</code></pre>

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

<pre><code>a
b
c
</code></pre>

python linux