Creating lists from rows with different lengths in python
<p>I am trying to create a list for each column in python of my data that looks like this:</p>

<pre><code>399.75833     561.572000000        399.75833     561.572000000  a_Fe I 399.73920 nm
399.78316     523.227000000        399.78316     523.227000000  
399.80799     455.923000000        399.80799     455.923000000  a_Fe I 401.45340 nm
399.83282     389.436000000        399.83282     389.436000000  
399.85765     289.804000000        399.85765     289.804000000  
</code></pre>

<p>The problem is that each row of my data is a different length. Is there anyway to format the remaining spaces of the shorter rows with a space so they are all the same length?</p>

<p>I would like my data to be in the form: </p>

<pre><code>list one= [399.75833, 399.78316, 399.80799, 399.83282, 399.85765]
list two= [561.572000000, 523.227000000, 455.923000000, 389.436000000, 289.804000000]
list three= [a_Fe, " ", a_Fe, " ", " "]
</code></pre>

<p>This is the code I used to import the data into python:</p>

<pre><code>fh  = open('help.bsp').read()
the_list = []
for line in fh.split('\n'):
    print line.strip()
    splits = line.split()
    if  len(splits) ==1 and splits[0]== line.strip():
        splits = line.strip().split(',')
    if splits:the_list.append(splits)
</code></pre>

<p>So, your lines are either whitespace delimited or comma delimited, and if comma delimited, the line contains no whitespace?  (note that if <code>len(splits)==1</code> is true, then <code>splits[0]==line.strip()</code> is also true).  That's not the data you're showing, and not what you're describing.</p>

<p>To get the lists you want from the data you show:</p>

<pre><code>with open('help.bsp') as h:
    the_list = [ line.strip().split() for line in h.readlines() ]
list_one = [ d[0] for d in the_list ]
list_two = [ d[1] for d in the_list ]
list_three = [ d[4] if len(d) &gt; 4 else ' ' for d in the_list ]
</code></pre>

<p>If you're reading comma separated (or similarly delimited) files, I always recommend using the <code>csv</code> module - it handles a lot of edge cases that you may not have considered.</p>

python