Python reading a tabbed text file into a series of lists
Question: Hi as an inexperieced python user I would appreciate any help with the
following programming challenge: I have a text file with tabulated data, I
want to read it and put the values on each line into different python lists.
The file looks like this:

    
    
     1  303233.479  233942.326     52.500       0.000  97 47 39.5    INFINITY     0.00034     0.00000 PBT  PBT  A001 B001  
     2  303386.031  233921.445     52.553     153.975  97 47 39.5    INFINITY     0.00034     0.00000 TS        A001
     3  303397.931  233919.897     52.557     165.975  96 38 54.2     -300.000    0.00034     0.00000 SC        A002
     4  303405.224  233919.137     52.559     173.308  95 14 52.6     -300.000    0.00034     6.25000      PC        B002
    

There are 13 colums and I want to put the values into 13 lists, I understand
how to do this for a couple of colums but I am a bit stumped at how to do this
for 13 colummns.

    
    
    #Here is my pathetic attempt at this
    
    pntnums = [] #a
    xcogo = [] #b
    ycogo = [] #c
    zcogo = [] #d
    chain = [] #e
    bearing = [] #f
    rad = [] #g
    grad = [] #h
    mval = [] #i
    HCOD = [] #j
    VCOD = [] #k
    
    fd = file("align.txt").readlines():
        a, b, c, d, e, f, g, h, i, j, k,  = [int(s) for s in l.split()]  
        pntnums.append(int(a))  
        xcogo.append(int(b))  
        ycogo.append(int(c)) 
        zcogo.append(int(d)) 
        chain.append(int(e)) 
        bearing.append(int(f)) 
        rad.append(int(g))  
        grad.append(int(h))  
        mval.append(int(i)) 
        HCOD.append(int(j)) 
        VCOD.append(int(k)) 
    for val in pntnums:
        print val 
    
    #and the corresponding output:
    Traceback (most recent call last):
      File "C:\MYPY\test.py", line 2, in <module>
        dataDict = dict(zip([float(i[1]) for i in data], [j[0] for j in data]))
    IndexError: list index out of range
    

Any help on this would be most appreciated (evan a url), as I have searched
and could not find a solution.

newuser


Answer: You should use a
[`csv.reader`](http://docs.python.org/library/csv.html#csv.reader); this is a
built-in class in Python designed specifically for reading files like this.

    
    
    >>> import csv
    >>> fieldnames = ("pntnums", "xcogo", "ycogo", "zcogo", "bearing",
                      "rad", "grad", "mval", "HCOD", "VCOD")
    >>> reader = csv.DictReader(open(...), delimiter="\t", fieldnames=fieldnames)
    

You can then iterate over the elements of `reader` and it will give you
dictionaries:

    
    
    >>> import pprint
    >>> for row in reader:
    ...     pprint.pprint(row)
    ...
    {None: ['0.00000', 'PBT PBT', 'A001 B001  '],
     'HCOD': 'INFINITY',
     'VCOD': '0.00034',
     'bearing': '0.000',
     'grad': '47',
     'mval': '39.5',
     'pntnums': '1',
     'rad': '97',
     'xcogo': '303233.479',
     'ycogo': '233942.326',
     'zcogo': '52.500'}
    {None: ['0.00000', 'TS', 'A001'],
     'HCOD': 'INFINITY',
     'VCOD': '0.00034',
     'bearing': '153.975',
     'grad': '47',
     'mval': '39.5',
     'pntnums': '2',
     'rad': '97',
     'xcogo': '303386.031',
     'ycogo': '233921.445',
     'zcogo': '52.553'}
    {None: ['0.00000', 'SC', 'A002'],
     'HCOD': '-300.000',
     'VCOD': '0.00034',
     'bearing': '165.975',
     'grad': '38',
     'mval': '54.2',
     'pntnums': '3',
     'rad': '96',
     'xcogo': '303397.931',
     'ycogo': '233919.897',
     'zcogo': '52.557'}
    {None: ['6.25000', 'PC', 'B002'],
     'HCOD': '-300.000',
     'VCOD': '0.00034',
     'bearing': '173.308',
     'grad': '14',
     'mval': '52.6',
     'pntnums': '4',
     'rad': '95',
     'xcogo': '303405.224',
     'ycogo': '233919.137',
     'zcogo': '52.559'}
    

(The data probably don't match up exactly with the fields here, because I
don't have the original tab-separated text, just what I can copy-paste from
SO. It will work if you feed it the original file =).)

