Search code examples
python-3.xsubstringregexp-replace

Delete words from line and save the line in file with python


I need the following line : test_list="root/abc_test.list someText tests/long_test.list someText tests/h2.list"

to be changed to :

test_list="tests/My_test.list" 

**the .list may appear anywhere in the line .

actually what need to be done is find each word that has ".list and if it is long_test.list to change it to My_test.list , and after its changed , then remove all other substrings that contains ".list" accept the one that was changed .

in order to solve this I've changed the word long_test.list to xXxXxX, then removed all the .list words, and then changed the xXxXxX back to My_test.list . for doing it I've used the following code:

  with in_place.InPlace('RUN_NEW') as file:
      for line in file:
          line = line.replace('long_test_list', 'xXxXxX')
          file.write(line)
      file.close()

  with open("RUN_NEW","r+") as f:
      new_f = f.readlines()
      f.seek(0)
      for line in new_f:
          line = str(line).split(' ')
          print(line)
          print(len(line))
          for word in line:
              if ".list" not in word:
                  f.write(word)
      f.close()

   with open("file.txt","r+") as f:
      new_f = f.readlines()
      f.seek(0)
      for line in new_f:
          line = str(line).split(' ')

          for k in line:
               if ".list" not in k:
                  s=' '
                  print(s.join(k.split('"')))
                  #f.write(s.join(k.split('"')))

The issue with the above code , is that the line after the split by (' ') looks like ['set', 'test_list="tests/long_test.list', 'tests/h2.list"'], when I am trying to join it back, it saves it with "\n" after each substring that don't contains .list . I was thinking about regular expression, but that would only remove the .list itself, an all the substring . and also I would like to save the quotes without them being deleted during the splitting.


Solution

  • This little script demonstrates how to do the replacement in one line. Use re.sub to look at the line read in (simulated here) and look for a double quote followed by an optional group of any number of characters ending in a space (Optional in case the target path is at the start of the string) followed by a captured group of any number of characters followed by the target literal string "long_test.list" followed by any number of characters ending with a double quote. Replace that entire match if found with a double quote followed by the second capture group, followed by the literal "My_test.list" then a closing double quote. If the match is NOT found, the entire line as read in will be returned.

    #!/usr/bin/env python
    
    import re
    
    line = 'test_list="root/abc_test.list someText tests/long_test.list someText tests/h2.list"'
    
    print(line)
    
    line = re.sub(r'"(.* )?(.*)long_test.list.*"', r'"\2My_test.list"', line)
    
    print(line)
    
    $ ./test.py
    test_list="root/abc_test.list someText tests/long_test.list someText tests/h2.list"
    test_list="tests/My_test.list"
    $