Dealing with text file containing list and None values
<p>I have a text file similar to this one:</p>

<pre><code>a, 1, 2.5, 3
b, 1, 1, 1 1 2 3 4, 1 2, 3
c 1, 2, 2, 2, 2, None, 2
</code></pre>

<p>To put it in a nutshell each row starts with its name and is followed by a variable number of floats or list of floats delimited by commas and some float can be None values.
and I would like to parse it to get something similar to a dictionnary D</p>

<pre><code>D['a']=[1,2.,3]
D['b']=[1,1,[1,1,2,3,4],[1,2],3]
D['c']=[1,2,2,2,2,None,2]
</code></pre>

<p>Basically I could have used <code>numpy.loadtxt</code> or <code>numpy.genfromtxt</code> and played with their options but there is not the same number of column in each row for genfromtxt or there are string values.<br>
The csv module is helpful but still requires to do engineering on the resulting rows.<br>
Do I need to use IO stream/csv and do that manually or is there some clean pythonic way of doing it ?
for now my ugly solution looks like this:</p>

<pre><code>import csv
f=open("text.txt","rb")
reader=csv.reader(f)
D={}
for row in reader:
  if len(row)!=0:
    if row[0]=="a":
      D["a"]=row[1:]
    elif row[0]=="b":
</code></pre>

<p>and so on but now I have got to parse convert strings to number and list and floats.  </p>

<p><strong>EDIT 1</strong> With @Daniel Lee's answer and this poorly written hack it works but clearly I am not doing things right:</p>

<pre><code>def convert list_to_float(L):
  s=[None]*len(L)       
  for index, l in enumerate(L):
    if len(l.split())&gt;1:
        s[index]=[float(e) for e in l.split()]
    elif 'None' in l:
        s[index]=None
    else:
        s[index]=float(l)
  return s
</code></pre>

<p>Try this, D is a dictionary, then each row will use its 1st letter as the key and the rest of the list as the value.</p>

<pre><code>import csv

with open('items.csv', 'rB') as f:
 csv_reader = csv.reader(f)
 for row in csv_reader:
     try:
         D[row[0]] = [float(x) for x in row[1:]]
     except ValueError as e:
         D[row[0]] = [float(f) for f in x.split() if f!='None' for x in row[1:]]
</code></pre>

<p>This should do what you want.</p>

python csv numpy