How to remove empty separators from read files in Python?
<p>Here is my input file sample (z.txt)</p>

<pre><code>&gt;qrst
ABCDE--  6  6 35 25 10
&gt;qqqq
ABBDE--  7  7 28 29  2
</code></pre>

<p>I store the alpha and numeric in separate lists. Here is the output of numerics list
     #Output : ['', '6', '', '6', '35', '25', '10']
               ['', '7', '', '7', '28', '29', '', '2']</p>

<p>The output has an extra space when there are single digits because of the way the file has been created. Is there anyway to get rid of the '' (empty spaces)?</p>

<p>I guess there are many ways to do this. I prefer using regular expressions, although this might be slower if you have a large input file with tens of thousands of lines. For smaller files, it's okay. </p>

<p>Few points:</p>

<ol>
<li><p>Use context manager (<code>with</code> statement) to open files. When the <code>with</code> statement ends, the file will automatically be closed.</p></li>
<li><p>An alternative to <code>re.findall()</code> is <code>re.match()</code> or <code>re.search()</code>. Subsequent code will be slightly different.</p></li>
<li><p>It <code>org</code>, <code>sequence</code> and <code>numbers</code> are related element-wise, I suggest you maintain a list of 3-element tuples instead. Of course, you have buffer the org field and add to the list of tuples when the next line is obtained.</p>

<pre><code>import re

org = []
sequence = []
numbers = []

with open('ddd', 'r') as f:
    for line in f.readlines():
        line = line.strip()
        if re.search(r'^&gt;', line):
            org.append(line)
        else:
            m = re.findall(r'^([A-Z]+--)\s+(.*)\s+', line)
            if m:
                sequence.append(m[0][0])
                numbers.append(map(int, m[0][1].split())) # convert from str to int

print(org, sequence, numbers)
</code></pre></li>
</ol>

python input split emptydatatext