Parsing of a file

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Aug 6 23:28:44 CEST 2008


Using something like PyParsing is probably better, but if you don't
want to use it you may use something like this:

raw_data = """
Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797690 Frames
5 Set 1
Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811620 Frames
5 Set 2
Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823860 Frames
5 Set 3
Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836020 Frames
5 Set 4
Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848210 Frames
5 Set 5
Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860400 Frames
5 Set 6
Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872620 Frames
5 Set 7
Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884890 Frames
5 Set 8
Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897070 Frames
5 Set 9
Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909350 Frames
5 Set 10"""

# from each line extract the fields: id, ra, dec, mjd
# even if they are swapped

data = []
for line in raw_data.lower().splitlines():
    if line.startswith("field"):
        parts = line.split()
        record = {"id": int(parts[1][1:-1])}
        for part in parts[2:]:
            if "=" in part:
                title, field = part.split("=")
                record[title] = field
        data.append(record)
print data

-----------------

Stefan Behnel:
>You can use named groups in a single regular expression.<

Can you show how to use them in this situation when fields can be
swapped?

Bye,
bearophile



More information about the Python-list mailing list