Changing line in file based on previous lines - Python
Question: Relatively new to python, trying to figure out the most general and readable
way to attack this problem. Execution speed wouldn't be bad either, but its a
secondary concern.

I have an input file for another program that I need to edit automatically.
The format is similar to the following:

    
    
    ---Thousands of lines that can be ignored---
    &Brand: Ford
    &Define Class
    &Model: Sedan
    &Parameter: Cost
    &Dollars
    &25000
    &Parameter: Stock
    &Quantity
    &14
    
    &Brand: Honda
    &Define Class
    &Model: Sedan
    &Parameter: Cost
    &Dollars
    &22000
    &Parameter: Stock
    &Quantity
    &17
    &Model: SUV
    &Parameter: Cost
    &Dollars
    &35000
    &Parameter: Stock
    &Quantity
    &7
    ---Thousands of lines that can be ignored---
    

My code needs to automatically change the numerical parameters. The trouble
I'm having is that I'm not just matching a single condition and changing a
line, I'm matching unique combinations of non-unique lines (Parameter: Cost
appears three times, twice under Model: Sedan and twice under Brand: Honda,
but only once under both of those conditions).

Right I'm storing the new parameters in a nested dictionary, like:

    
    
    params = {'Ford': {'Sedan': {'Cost': 17000, 'Stock': 43}}, 'Honda':{'Sedan': {'Cost': 19000, 'Stock': 12}, {'Truck': {'Cost': 33000, 'Stock': 5}}}
    

In this way I can do `for brand in params.keys()` and then `for model in
params[brand].keys()` and so on.

I have the basics of opening, closing, and modifying files down, it is
identifying the correct lines to be modified that I'm having trouble with.
Thanks for any help.

Ex: For the above sample dictionary, the ideal output would be:

    
    
    ---Thousands of lines that can be ignored---
    &Brand: Ford
    &Define Class
    &Model: Sedan
    &Parameter: Cost
    &Dollars
    &17000
    &Parameter: Stock
    &Quantity
    &43
    
    &Brand: Honda
    &Define Class
    &Model: Sedan
    &Parameter: Cost
    &Dollars
    &19000
    &Parameter: Stock
    &Quantity
    &12
    &Model: SUV
    &Parameter: Cost
    &Dollars
    &33000
    &Parameter: Stock
    &Quantity
    &5
    ---Thousands of lines that can be ignored---
    


Answer: Have to looked at Python's regular expressions? Take a look at the 're'
package. You can use that to search for numerical entries. You can identify
the lines of interest like this (from the top of my head and not checked):

    
    
    import re
    ...
    m = re.match(r'&(\d+)', the_line)
    if m:
        print 'found ', m.group(1)
        # modify it...
    

The expression matches any number of digits (the \d+ part). Not sure if & is
special, but if it is you can put in in square brackets.

Granted, You'll need similar regular expressions to capture that the line
before is cost, and then capture the value. You can do that with a simple flag
to signal net line is cost.

See <https://docs.python.org/2/library/re.html>

