Parsing Text file

Joshua Landau joshua.landau.ws at gmail.com
Tue Jul 2 22:56:55 CEST 2013


On 2 July 2013 21:28,  <sas429s at 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?



More information about the Python-list mailing list