Editing a file by substitution

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

    Editing a file by substitution

    Hi, what I'd like to do is edit an input file for a calculation with
    Python. Let's say that I have an input file like the following

    ------------------------------>>

    BLAH BLAH BLAH

    Other inputs, Volume 940 m^3, maybe some more stuff

    STUFF STUFF
    ------------------------------>>


    So let's say I want to edit this file and change Volume from 940 to 950.

    This is how I would guess I would do something like it:

    ---------------------------------------------

    import re
    expr=r'.*Volume \s+(?P<VolValue >\d+[.]?[\d+]?)'
    exprComp=re.com pile(expr)
    infile=open('my file','r')
    outfile=open('o utfile','w')

    for line in infile:
    match=exprComp. search(line)
    if match:
    newline=<HELP ME!>
    print >outfile, newline
    else:
    print >outfile, line
    infile.close()
    outfile.close()
    ------------------------------

    So what I would like to do in the section labeled <HELP ME!is to use
    something like the re.sub() function and replace the contents of the
    "VolValue" group with another number. However I am not sure how exactly
    to do this. I feel like I could use the information that's on this
    website:http://www.regular-expressions.info/python.html to do it but it
    seems that I can only USE captured groups, not REPLACE them. This makes
    it seem to me that I would need my groups to be everything that I WASN'T
    going to replace. Then I could do something like this:

    re.sub(regex,\g <StuffBeforeVol umeValue>950\g< StuffAfterVolum eValue>)

    or something along those lines. Although that is really so much uglier
    than if i could simply replace the captured group.

    Please help pythonistas :)

  • alex23

    #2
    Re: Editing a file by substitution

    On Aug 12, 12:03 pm, Brad <bmal...@berkel ey.eduwrote:
    So let's say I want to edit this file and change Volume from 940 to 950.
    Personally, I'd recommend avoiding re and sticking with the standard
    string functions.

    Something like this should be pretty effective:
    >>open('outfile ','w').writelin es(l.replace('V olume 940','Volume 950') forl in open('myfile',' r'))

    Comment

    • Brad

      #3
      Re: Editing a file by substitution

      alex23 wrote:
      On Aug 12, 12:03 pm, Brad <bmal...@berkel ey.eduwrote:
      >So let's say I want to edit this file and change Volume from 940 to 950.
      >
      Personally, I'd recommend avoiding re and sticking with the standard
      string functions.
      >
      Something like this should be pretty effective:
      >
      >>>open('outfil e','w').writeli nes(l.replace(' Volume 940','Volume 950') for l in open('myfile',' r'))
      Ahh yes, perhaps string function are simpler to use in this case.
      Thanks Alex!

      Comment

      • Terry Reedy

        #4
        Re: Editing a file by substitution



        Brad wrote:
        alex23 wrote:
        >>>>open('outfi le','w').writel ines(l.replace( 'Volume 940','Volume 950')
        >>>>for l in open('myfile',' r'))
        >
        Ahh yes, perhaps string function are simpler to use in this case.
        Thanks Alex!
        If, if you have to change the file repeatedly, make it a template
        (either % or the new .format variety) and substitute in the value you want.

        Comment

        Working...