简体   繁体   中英

Python: How to remove (replace with "") combinations of apostrophes and commas in Python strings

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.

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
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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM