Re: Parsing of a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shawn Milochik

    #1

    Re: Parsing of a file

    >
    I would like to parse this file by extracting the field id, ra, dec and mjd
    for each line. It is
    not, however, certain that the width of each value of the field id, ra, dec
    or mjd is the same
    in each line. Is there a way to do this such that even if there was a line
    Regular expressions will do the trick nicely.
    where Ra=****** and
    MJD=******** was swapped it would be parsed correctly?
    >
    Yes, but you'll probably have to look at each line three times or
    split it into a list and check the elements.


    ShawnMilo
  • Stefan Behnel

    #2
    Re: Parsing of a file

    Shawn Milochik wrote:
    >I would like to parse this file by extracting the field id, ra, dec and mjd
    >for each line. It is
    >not, however, certain that the width of each value of the field id, ra, dec
    >or mjd is the same
    >in each line. Is there a way to do this such that even if there was a line
    >
    Regular expressions will do the trick nicely.
    >
    >where Ra=****** and
    >MJD=******** was swapped it would be parsed correctly?
    >>
    >
    Yes, but you'll probably have to look at each line three times or
    split it into a list and check the elements.
    You can use named groups in a single regular expression.

    Stefan

    Comment

    • Paul McGuire

      #3
      Re: Parsing of a file

      On Aug 6, 3:14 pm, "Shawn Milochik" <Sh...@Milochik .comwrote:
      Regular expressions will do the trick nicely.
      >
      Or just use str.split, and create dicts using dict(list_of_tu ples)
      constructor.

      This code creates a single dict for the input lines, keyed by id.
      Each value contains elements labeled 'id', 'ra', and 'mjd'.

      -- Paul


      data = """\
      Field f29227: Ra=20:23:46.54 Dec=+67:30:00.0 MJD=53370.06797 690
      Frames 5 Set 1
      Field f31448: Ra=20:24:58.13 Dec=+79:39:43.9 MJD=53370.06811 620
      Frames 5 Set 2
      Field f31226: Ra=20:24:45.50 Dec=+78:26:45.2 MJD=53370.06823 860
      Frames 5 Set 3
      Field f31004: Ra=20:25:05.28 Dec=+77:13:46.9 MJD=53370.06836 020
      Frames 5 Set 4
      Field f30782: Ra=20:25:51.94 Dec=+76:00:48.6 MJD=53370.06848 210
      Frames 5 Set 5
      Field f30560: Ra=20:27:01.82 Dec=+74:47:50.3 MJD=53370.06860 400
      Frames 5 Set 6
      Field f30338: Ra=20:28:32.35 Dec=+73:34:52.0 MJD=53370.06872 620
      Frames 5 Set 7
      Field f30116: Ra=20:30:21.70 Dec=+72:21:53.6 MJD=53370.06884 890
      Frames 5 Set 8
      Field f29894: Ra=20:32:28.54 Dec=+71:08:55.0 MJD=53370.06897 070
      Frames 5 Set 9
      Field f29672: Ra=20:34:51.89 Dec=+69:55:56.6 MJD=53370.06909 350
      Frames Set 10 """.splitlines( )

      d = dict(
      (rec.split()[1][:-1],
      dict([('id',rec.split ()[1][:-1])] +
      [map(str.lower,f .split('='))
      for f in rec.split()[2:5]] ) )
      for rec in data
      )
      print d.keys()

      for id in d.keys():
      print d[id].items()

      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: Parsing of a file

        Paul McGuire:
        This code creates a single dict for the input lines, keyed by id.
        Each value contains elements labeled 'id', 'ra', and 'mjd'.
        ....
        d = dict(
        (rec.split()[1][:-1],
        dict([('id',rec.split ()[1][:-1])] +
        [map(str.lower,f .split('='))
        for f in rec.split()[2:5]] ) )
        for rec in data
        )
        ....

        Please, don't try to shove the program in one line, use PyParsing :o)

        Bye,
        bearophile

        Comment

        Working...