Danny wrote:
| Hi Chris,
| 
| Yes, I suspect that this happens a lot.  I have my own little formatting 
| reader that simulates some of the features of C's scanf, for example:
| 
|    http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/
| 
| so I think it's one of those little exercises that everyone ends up
| doing at least once.  *grin*

I've always taken a personal pride in (eventually) following the in the 
footsteps of those that have gone before me. There is certainly a joy in 
rediscovery :-)  (There is also a lot to learn from those whose stride is 
greater!)

I was able to pretty easily take your sscanf facility and couple it with the 
template reader to read Andrzej Kolinski's data. FWIW, here it is:

#####
data = '''1 Polonijna Liga Mistrzow
26 wrzesnia 2005
 6 12 6 4 1
 0 1 0
Bohossian - Kolinski
1 
      1.000 9 13 19
      2.000 2 4 16
      1.000 10 8 17
      0.000 8 6 17
Szadkowska - Szczurek
2
      0.000 11 16 20
      3.000 1 -4 14
      3.500 3 -7 13
      2.500 10 13 19
and then here is single line
'''.split('\n')
lines = iter(data)

template1 = '''\
_
_
< %*s %*s %d %d %*s> #same sort of format as before, but now an explicit token 
identification occurs
_'''
template2 = '''\
<%s %*s %s>Bohossian - Kolinski
_1 
< %*s %*s %i %*s>      1.000 9 13 19 # I get around having to worry about the 
decimal by using %s
< %*s %*s %i %*s>      2.000 2 4 16
< %*s %*s %i %*s>      1.000 10 8 17
< %*s %*s %i %*s>      0.000 8 6 17'''

if __name__ == '__main__':
    def tread(template, lines):
        #reading lines using lines of the template to parse them if the 
        #lines start with "<"
        rv = []
        try:
            for i,li in enumerate(template.splitlines()):
                dat = lines.next()
                if li.startswith('<'):
                    fmt = li.split('>')[0][1:] #take everything between the <>
                    rv.extend(sscanf(dat, fmt))
            return rv
        except:
            #print 'error at line',i
            #print 'in template:'
            #print template1
            return None
        
    print tread(template1, lines)
    while True:
        vals = tread(template2, lines)
        if not vals: break
        print vals
######

Hey, Andrzej, it will be interesting to see what you come up with as a 
solution. This has been a helpful problem for me :-)

/c
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to