Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49656

Re: Parsing Text file

References (1 earlier) <b3gnnaFbe60U1@mid.individual.net> <8ea32ea7-2cee-4e61-8cbd-066721d88d4a@googlegroups.com> <I9GAt.794$ct1.646@newsfe07.iad> <b3gqi7FbvfsU1@mid.individual.net> <7e82becd-77c1-4800-8f4e-7624b19de82b@googlegroups.com>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date 2013-07-02 21:56 +0100
Subject Re: Parsing Text file
Newsgroups comp.lang.python
Message-ID <mailman.4126.1372798662.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 2 July 2013 21:28,  <sas429s@gmail.com> wrote:
> Here I am looking for the line that contains: "WORK_MODE_MASK", I want to print that line as well as the file name above it: config/meal/governor_mode_config.h
> or config/meal/components/source/ceal_PackD_kso_aic_core_config.h.
>
> SO the output should be something like this:
> config/meal/governor_mode_config.h
>
> #define GOVERNOR_MODE_WORK_MODE_MASK    (CEAL_MODE_WORK_MASK_GEAR| \
>                                            CEAL_MODE_WORK_MASK_PARK_BRAKE | \
>                                            CEAL_MODE_WORK_MASK_VEHICLE_SPEED)
>
> config/meal/components/source/kso_aic_core_config.h
> #define CEAL_KSO_AIC_WORK_MODE_MASK   (CEAL_MODE_WORK_MASK_GEAR       | \
>                                    CEAL_MODE_WORK_MASK_PARK_BRAKE | \
>                                    CEAL_MODE_WORK_MASK_VEHICLE_SPEED)

(Please don't top-post.)

    filename = None

    with open("tmp.txt") as file:
        nonblanklines = (line for line in file if line)

        for line in nonblanklines:
            if line.lstrip().startswith("#define"):
                defn, name, *other = line.split()
                if name.endswith("WORK_MODE_MASK"):
                    print(filename, line, sep="")

            else:
                filename = line

Basically, you loop through remembering what lines you need, match a
little bit and ignore blank lines. If this isn't a solid
specification, you'll 'ave to tell me more about the edge-cases.

You said that

> #define CEAL_KSO_AIC_WORK_MODE_MASK   (CEAL_MODE_WORK_MASK_GEAR       | \
>                                    CEAL_MODE_WORK_MASK_PARK_BRAKE | \
>                                    CEAL_MODE_WORK_MASK_VEHICLE_SPEED)

was one line. If it is not, I suggest doing a pre-process to "wrap"
lines with trailing "\"s before running the algorithm:

    def wrapped(lines):
        wrap = ""
        for line in lines:
            if line.rstrip().endswith("\\"):
                wrap += line

            else:
                yield wrap + line
                wrap = ""

...
        nonblanklines = (line for line in wrapped(file) if line)
...


This doesn't handle all wrapped lines properly, as it leaves the "\"
in so may interfere with matching. That's easily fixable, and there
are many other ways to do this.

What did you try?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Parsing Text file sas429s@gmail.com - 2013-07-02 11:45 -0700
  Re: Parsing Text file Neil Cerutti <neilc@norwich.edu> - 2013-07-02 19:24 +0000
    Re: Parsing Text file sas429s@gmail.com - 2013-07-02 12:30 -0700
      Re: Parsing Text file Tobiah <toby@tobiah.org> - 2013-07-02 12:50 -0700
        Re: Parsing Text file Neil Cerutti <neilc@norwich.edu> - 2013-07-02 20:12 +0000
          Re: Parsing Text file sas429s@gmail.com - 2013-07-02 13:28 -0700
            Re: Parsing Text file Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-02 21:56 +0100
            Re: Parsing Text file Denis McMahon <denismfmcmahon@gmail.com> - 2013-07-03 00:55 +0000
        Re: Parsing Text file Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-02 21:28 +0100

csiph-web