quickly read a formated file?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Mar 1 10:07:42 CET 2007

> The formated file may be very popularly, but the module ConfigPaser
> doesn't handle it. Is there a way to process it freely?

First try, assuming the input file can be read whole. The code isn't
much readable, it needs better variable names (name names?), comments,

data = """
title1 = "Untilted1"
username = "User1"

title2 = "Untilted2"
username2 = "User2"

l1 = (p.strip().splitlines() for p in data.split("%") if p.strip())
result = {}
for part in l1:
    pairs1 = (pair.split('=') for pair in part[1:])
    pairs2 = ((k.strip(), v.strip().strip('"')) for k,v in pairs1)
    result[part[0]] = dict(pairs2)
print result

All done lazily for your better comfort :-)


More information about the Python-list mailing list