Python : split text to list of lines
<p>am new to Python , But i have text File like :</p>

<pre><code>12345 | 6789 | abcd | efgh 
</code></pre>

<p>i want my output be Like :</p>

<pre><code>12345
6789
abcd
efgh
</code></pre>

<p>=====================</p>

<p>i really don't know the script 
but i made a lot of scripts  by those function split() , strip() , blame blame blame </p>

<p>but i failed to do it 
so am asking for help is someone can .</p>

<p>i will appreciate any Help .</p>

<pre><code>with open('contacts_index1.txt') as f:
    lines = f.read().splitlines("|")
</code></pre>

<p>Please do this line by line. There is no need to read the entire file at once.</p>

<p>Something like:</p>

<pre><code>with open(file_name) as f_in:
    for line in f_in:
        for word in line.split('|'):
            print word.strip()
</code></pre>

<hr>

<p>If it is a unicode issue, most of the time it is automatic:</p>

<pre><code>$ cat /tmp/so.txt
12345 | 6789 | abcd | Ã©fgh 
</code></pre>

<p>(note the <code>Ã©</code> in the file)</p>

<p>The program above works. If it does NOT work, use a codec:</p>

<pre><code>with open(fn) as f_in:
    for line in f_in:
        line=line.decode('utf-8')  # or whatever codec is used for that file...
        for word in line.split('|'):
            print word.strip()
</code></pre>

<hr>

<p>With Python3, just set the encoding when you open the file:</p>

<pre><code>with open(fn, encoding='utf-8') as f_in:   # &lt;= replace with the encoding of the file...
    for line in f_in:
        for word in line.split('|'):
            print(word.strip())
</code></pre>

python text split lines