Home > Back-end >  Python: How to remove (replace with "") combinations of apostrophes and commas in Python s
Python: How to remove (replace with "") combinations of apostrophes and commas in Python s

Time:12-27

I have a string like this which has some weird characters attached to the front (Entire thing is a string):

# Edited to make more clear. It is essentially this:

x = """
INFO     2022-12-27 16:56:25.843 request id: app - xxxxxx
', "INFO     2022-12-27 16:56:26.407 request id: app - xxxxxxx
", 'INFO     2022-12-27 16:56:26.407 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.497 request id: app - xxxxxxxx
"""

# So need to remove the comma and apostrophes in the 2nd and 3rd line of string object x

I would like to remove the ', " and ', " from the 2nd and 3rd lines, but it is difficult to replace or regex it due to the combination of apostrophes creating string literal is unterminated errors.

CodePudding user response:

Assuming each log line would always begin with some keyword like INFO or DEBUG, we can do a regex replacement on the text in multiline mode:

logs = """INFO     2022-12-27 16:56:25.843 request id: app - xxxxxx
', \"INFO     2022-12-27 16:56:26.407 request id: app - xxxxxxx
\", 'INFO     2022-12-27 16:56:26.407 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.497 request id: app - xxxxxxxx"""

output = re.sub(r'^[^A-Z] ', '', logs, flags=re.M)
print(output)

This prints:

INFO     2022-12-27 16:56:25.843 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.407 request id: app - xxxxxxx
INFO     2022-12-27 16:56:26.407 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.497 request id: app - xxxxxxxx

CodePudding user response:

string_test = f"""INFO     2022-12-27 16:56:25.843 request id: app - xxxxxx
', "INFO     2022-12-27 16:56:26.407 request id: app - xxxxxxx
", 'INFO     2022-12-27 16:56:26.407 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.497 request id: app - xxxxxxxx"""

string_test = string_test.replace(f"'", "")
print(string_test)

How this works: Replace every ' with nothing, removing it.

Hopefully, this helps.

CodePudding user response:

Working on the assumption that what you really want to do is remove anything/everything that precedes 'INFO' on any line then:

mystring = """INFO     2022-12-27 16:56:25.843 request id: app - xxxxxx
', "INFO     2022-12-27 16:56:26.407 request id: app - xxxxxxx
", 'INFO     2022-12-27 16:56:26.407 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.497 request id: app - xxxxxxxx"""

newstring = []

for line in mystring.splitlines():
    if (i := line.find('INFO')) >= 0:
        line = line[i:]
    newstring.append(line)

print('\n'.join(newstring))

Output:

INFO     2022-12-27 16:56:25.843 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.407 request id: app - xxxxxxx
INFO     2022-12-27 16:56:26.407 request id: app - xxxxxx
INFO     2022-12-27 16:56:26.497 request id: app - xxxxxxxx

CodePudding user response:

Use the str.replace() method to remove all apostrophes from a string, e.g. result = my_str.replace("'", ''). The str.replace() method will remove all apostrophes from the string by replacing them with empty strings.

  • Related