Search code examples
pythonreplacenewlinedata-processing

Can Python perform the 'replace' function across lines?


I would like to replace only the 3rd } in the below script to ].

 "objectLocationHeight" : "46.500000",
    "text" : "1494"
  },
  "timestamp" : "2015-10-09 12:50:58",
  "eventType" : "keyboardOnAnsBox",
  "action" : "endTyping"
}
  },
"submitTime" : "

The following is the target result

 "objectLocationHeight" : "46.500000",
    "text" : "1494"
  },
  "timestamp" : "2015-10-09 12:50:58",
  "eventType" : "keyboardOnAnsBox",
  "action" : "endTyping"
}
  ],
"submitTime" : "

However, it seems that the 'replace' function can only work within a line and I couldn't get the desired result with

str1= str.replace("},\n\"submit","],\n\"submit")

May I know if there is other syntax that allows me to perform replace across lines?


Solution

  • If I understand correctly your question, you need to replace any } in ] in a document if it appears before "submitTime".

    You could use backwards document reading and a boolean flag to insure that the previous line was the relevant one:

    for line in reversed(open("filename").readlines()):
        cond = line.startswith("\"submitTime\"")
        if cond and line.startswith("},"):
            new = line.replace("},","],")
            print new
            cond = False
        else:
            print line
    

    Maybe there exist more elegant solutions, but this should work. Of course, you could substitute print by opening an output file and writing into it with fout.write(line).