Using python as a search and replace macro; how to implement an unknown value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nihilium
    New Member
    • Mar 2008
    • 16

    Using python as a search and replace macro; how to implement an unknown value

    I've searched a bit for this, but haven't stumbled upon anything that is useful for me yet. My question is pretty simple; I have the following script:

    inputFile = file('C:/Documents and Settings/example.txt', 'r')
    data = inputFile.read( )
    inputFile.close ()
    data = data.replace('t his x something', 'that x something else')
    outputFile = file ('C:/Documents and Settings/example.txt', 'w')
    outputFile.writ e(data)
    outputFile.clos e()


    Where x is an unknown decimal typically with three digits (e.g. 145.531) whose exact value is not predictable.

    How do I implement x?


    Thanks in advance.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by nihilium
    I've searched a bit for this, but haven't stumbled upon anything that is useful for me yet. My question is pretty simple; I have the following script:
    [code=Python]
    inputFile = file('C:/Documents and Settings/example.txt', 'r')
    data = inputFile.read( )
    inputFile.close ()
    data = data.replace('t his x something', 'that x something else')
    outputFile = file ('C:/Documents and Settings/example.txt', 'w')
    outputFile.writ e(data)
    outputFile.clos e()
    [/code]

    Where x is an unknown decimal typically with three digits (e.g. 145.531) whose exact value is not predictable.

    How do I implement x?


    Thanks in advance.
    Please use code tags in your posts. You code will generally be much more readable.

    You can use string formatting to accomplish your goal.[code=Python]
    >>> data = 'The data contains "this 345.543 something" which needs to be replaced.'
    >>> x = 345.54321
    >>> data.replace('t his %.3f something' % x, 'that %.3f something else' % x)
    'The data contains "that 345.543 something else" which needs to be replaced.'
    >>> [/code]If you do not know what x is beforehand:[code=Python]import re
    patt = re.compile(r'th is (\d+\.\d+) something')
    data = 'The data contains "this 0.56658 something" which needs to be replaced.'
    m = patt.search(dat a)
    if m:
    data1 = data.replace(m. group(0), 'that %s something else' % m.group(1))

    >>> data
    'The data contains "this 0.56658 something" which needs to be replaced.'
    >>> data1
    'The data contains "that 0.56658 something else" which needs to be replaced.'
    >>> [/code]

    Comment

    • nihilium
      New Member
      • Mar 2008
      • 16

      #3
      Originally posted by bvdet
      >>> [/code]If you do not know what x is beforehand:[code=Python]import re
      patt = re.compile(r'th is (\d+\.\d+) something')
      data = 'The data contains "this 0.56658 something" which needs to be replaced.'
      m = patt.search(dat a)
      if m:
      data1 = data.replace(m. group(0), 'that %s something else' % m.group(1))

      >>> data
      'The data contains "this 0.56658 something" which needs to be replaced.'
      >>> data1
      'The data contains "that 0.56658 something else" which needs to be replaced.'
      >>> [/code]
      Ok, thanks for the help so far. I'm not so familiar with Python, so I didn't quite get how to use the script.

      Let's say that the text that needs to be replaced is in the document C:/Documents and Settings/example.txt, so I want to replace the text in that document as an automated process by running this script. For example I want to translate English dates into another language: December 2 should become 2. desember, where the number is a wildcard.

      Comment

      • woooee
        New Member
        • Mar 2008
        • 43

        #4
        It was embedded in the code submitted by bvdet.
        x=2 ## the unknown date
        string_to_find= "December %d" % (x)
        print "The string to find is", string_to_find

        This is called string formatting. Here is link to the "Dive Into Python" page on string formatting http://www.diveintopyt hon.org/native_data_typ es/formatting_stri ngs.html

        Comment

        • nihilium
          New Member
          • Mar 2008
          • 16

          #5
          Originally posted by bvdet
          [code=Python]import re
          patt = re.compile(r'th is (\d+\.\d+) something')
          data = 'The data contains "this 0.56658 something" which needs to be replaced.'
          m = patt.search(dat a)
          if m:
          data1 = data.replace(m. group(0), 'that %s something else' % m.group(1))

          >>> data
          'The data contains "this 0.56658 something" which needs to be replaced.'
          >>> data1
          'The data contains "that 0.56658 something else" which needs to be replaced.'
          >>> [/code]
          I finally understood this script, and it turns out it's just what I was looking for. Thanks!

          Comment

          Working...