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>I'm sure others will quickly point out the cause of this difference (it's related to newline characters), but since you asked 'How can I debug this?' I'd like to address that question:</p>

<p>Since the only difference between the passing and the failing run are the lists themselves, I'd concentrate on those. There is some difference between the lists (i.e. at least one differing list element) which triggers this. Hence, you could perform a binary search to locate the first differing list element triggering this.</p>

<p>To do so, just chop the lists in halves, e.g. take the first 317858/2 lines of each list. Do you still observe the same symptom? If so, repeat the exercise with that first half. Otherwise, repeat that exercise with the second half. That way, you'll need at most 19 tries to identify the line which triggers this. And at that point, the issue is simplified to a single string.</p>

<p>Chances are that you can spot the issue by just looking at the strings, but in principle (e.g. if the strings are very long), you could then proceed to do a binary search on those strings to identify the first character triggering this issue.</p>

python linux