[Tutor] text processing lines variable content

Peter Otten __peter__ at web.de
Thu Feb 7 03:29:16 EST 2019


ingo janssen wrote:

> depending on how the input file is created data packet a can be in an
> other position for every line.
> figured out how to do it though
> 
> order=[a,b,e,d...]
> for i in lines:
>    i=i.split(" ")
>      for j in order:
>        if j = a:
> use function for processing data chunk a
>        elseif j = b:
>          use proper function for processing data type b
>        ...

Where will you get the order from? If you plan to specify it manually, e. g.

lookup_steps = {
    "foo": [a, b, c, ...],
    "bar": [a, a, f, ...],
}
fileformat =  sys.argv[1]
steps = lookup_steps[fileformat]
...
for line in lines:
    for step in steps:
        if step == a:
            ...
        elif step == b:
            ...

then I recommend storing one function per file format instead:

def process_foo(line):
    ...  # process one line in foo format

def process_bar(line):
    ...

lineprocessors = {
    "foo": process_foo,
    "bar": process_bar,
}
fileformat =  sys.argv[1]
process = lineprocessors[fileformat]
...
for line in lines:
    process(line)

That way you deal with Python functions instead of a self-invented 
minilanguage.



More information about the Tutor mailing list