简体   繁体   English

Python:如何删除(替换为“”)Python 字符串中撇号和逗号的组合

[英]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.我想从第 2 行和第 3 行中删除', "', " ,但由于撇号的组合导致string literal is unterminated错误,因此很难替换或正则表达式。

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:假设每个日志行总是以某个关键字开头,如INFODEBUG ,我们可以在多行模式下对文本进行正则表达式替换:

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:假设您真正想做的是删除任何行上“INFO”之前的任何内容/所有内容,然后:

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: 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("'", '').使用 str.replace() 方法从字符串中删除所有撇号,例如 result = my_str.replace("'", '')。 The str.replace() method will remove all apostrophes from the string by replacing them with empty strings. str.replace() 方法将通过用空字符串替换它们来删除字符串中的所有撇号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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