data parsing

Raymond Hettinger othello at javanet.com
Sun Feb 25 01:43:53 EST 2001


Gnanasekaran Thoppae wrote:

> I have some data in a file 'test', which contains:
>
> Joe|25|30|49|40|
> |28|39|71||
> |30|29|||
> Malcolm|43|60|56||
> |28|37|||
> Amy||70|45||
> |40|30||40
> |40||30||
>
> I want to parse this data and format it in this way:
>
> Joe|25;28;30|30;39;29|49;71|40|
> Malcolm|43;28|60;37|56||
> Amy|40;40|70;30|45;;30|;40;|
>

name = 'unnamed'        # just in-case the file doesn't start with a name
result = {}
for line in inp.split():
    field = line.split('|')
    while len(field) < 6:   # fix misformatted line |40|40||40
        field.append('')
    k = field.pop(0)        # grab name field
    field.pop()             # clear final blank
    if k != '':             # on name change
        name = k
    if not result.has_key(name):
        result[name] = []
    result[name].append(field)

for name in result.keys():
    ans = apply( zip, result[name] )  # tranpose matrix
    ans = map( ';'.join, ans )
    ans.insert(0,name)
    ans.append('')
    print '|'.join(ans)

Raymond




More information about the Python-list mailing list