有没有Python函数可以删除反斜杠单引号

问题描述 投票:0回答:1

用于删除具有多个单引号的大双引号字符串中的反斜杠单引号 ' 的 Python 代码,并且在该单引号内我们有反斜杠单引号。

Input:
"INSERT INTO `a3c_parser_events_log` VALUES (1222,'A2540','AUDI','AP1','error','ERROR frontend_clang: invalid line marker flag \'2\' : cannot pop empty include stack , ','2023-05-11 10:19:33.724334','USER3'), (1279,'A2566','Nikola','ALL','error','ERROR frontend_clang: \'C:\\Users\\USER1\\Documents\\EXECUTION\\PATH\\TO\\FOLDER\\SOMETHING\\FOLDER\' file not found , ','2023-05-19 14:17:47.188004','USER1');"

输出应该是

Output:
"INSERT INTO `a3c_parser_events_log` VALUES (1222,'A2540','ERROR frontend_clang: invalid line marker flag 2 : cannot pop empty include stack , ','2023-05-11 10:19:33.724334','USER3'), (1279,'A2566','ERROR frontend_clang: C:\\Users\\USER1\\Documents\\EXECUTION\\PATH\\TO\\FOLDER\\SOMETHING\\FOLDER file not found , ','2023-05-19 14:17:47.188004','USER1');"

我尝试了下面的代码,但它不起作用

import re

def replace_quotes(text):
   # Use regular expression to find and replace the pattern \'text\' with "text"
   return text.replace("\\'", "")
python regex string python-re python-regex
1个回答
0
投票

您不应该使用 2 个反斜杠。您可以删除一个反斜杠并保留另一个反斜杠,也可以删除所有反斜杠

import re

def replace_quotes(text):
   # Use regular expression to find and replace the pattern \'text\' with "text"
   return text.replace("'", "")
© www.soinside.com 2019 - 2024. All rights reserved.