paseline(my favorite simple script): does something similar exist?

Gerard Flanagan grflanagan at yahoo.co.uk
Fri Oct 13 00:01:25 CEST 2006


RickMuller wrote:

> One of my all-time favorite scripts is parseline, which is printed
> below
>
> def parseline(line,format):
>     xlat = {'x':None,'s':str,'f':float,'d':int,'i':int}
>     result = []
>     words = line.split()
>     for i in range(len(format)):
>         f = format[i]
>         trans = xlat.get(f,'None')
>         if trans: result.append(trans(words[i]))
>     if len(result) == 0: return None
>     if len(result) == 1: return result[0]
>     return result
>
> This takes a line of text, splits it, and then applies simple
> formatting characters to return different python types. For example,
> given the line
>
> H    0.000   0.000   0.000
>
> I can call parseline(line,'sfff') and it will return the string 'H',
> and three floats. If I wanted to omit the first, I could just call
> parseline(line,'xfff'). If I only wanted the first 0.000, I could call
> parseline(line,'xf').
[...]
> I would love to hear how other people do similar things.
>
> Rick

MAP = {'s':str,'f':float,'d':int,'i':int}

def parseline( line, format, separator=' '):
    '''
    >>> parseline('A 1 2 3 4', 'sdxf')
    ['A', 1, 3.0]
    '''
    mapping = [ (i, MAP[f]) for (i,f) in enumerate(format) if f != 'x'
]
    parts = line.split(separator)
    return [f(parts[i]) for (i,f) in mapping]

def parseline2( line, format):
    '''
    >>> parseline('A 1 2 3 4', 'sdxf')
    ['A', 1, 3.0]
    '''
    return [f(line.split()[i]) for (i,f) in [(i, MAP[f]) for (i,f) in
enumerate(format) if f != 'x']]

def parselines(lines, format, separator=' '):
    '''
    >>> lines = [ 'A 1 2 3 4', 'B 5 6 7 8', 'C 9 10 11 12']
    >>> list(parselines(lines, 'sdxf'))
    [['A', 1, 3.0], ['B', 5, 7.0], ['C', 9, 11.0]]
    '''
    mapping = [ (i, MAP[f]) for (i,f) in enumerate(format) if f != 'x'
]
    for line in lines:
        parts = line.split(separator)
        yield [f(parts[i]) for (i,f) in mapping]


import doctest
doctest.testmod(verbose=True)




More information about the Python-list mailing list