Reading from a file and converting it into a list of lines: code not working

K.S.Sreeram sreeram at tachyontech.net
Tue Jun 6 00:40:42 EDT 2006


Girish Sahani wrote:
> 1) A dictionary with the numbers as keys and the letters as values.
> e.g the above would give me a dictionary like
> {1:'a', 2:'b', 3:'a', 5:'c', 6:'c' ........}

def get_dict( f ) :
    out = {}
    for line in file(f) :
        n1,s1,n2,s2 = line.split(',')
        out.update( { int(n1):s1[1], int(n2):s2[1] } )
    return out

> 2) A list containing pairs of numbers from each line.
> The above formmat would give me the list as
> [[1,2],[3,5],[3,6][3,7][8,7]......]

def get_pairs( f ) :
    out = []
    for line in file(f) :
        n1,_,n2,_ = line.split(',')
        out.append( [int(n1),int(n2)] )
    return out

Regards
Sreeram

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20060606/643e7ad6/attachment.sig>


More information about the Python-list mailing list