我有这个字符串,我从API请求中获取...
It was quick and great ! And it\\\\'s customized
我想删除所有4个反斜杠(在撇号之前)。
以下是代码所在的地方......
reviews = reviews_response.json()
filtered_reviews = []
for key, review in reviews.items():
comment = review.get('textcomments')
comment = comment.replace(r"\\'", "'")
if len(comment) > 30:
filtered_reviews.append(review)
在这个特定的例子中,当我在PyCharm中逐步执行它时,变量comment
在replace()
执行后永远不会改变。我尝试过这些事情却没有成功......
regex = re.compile(r"\\\\")
comment = "It was quick and great ! And it\\\\'s customized"
comment = regex.sub('', comment)
comment = re.sub(r"(\\){4}", '', comment)
comment = re.sub(r"\\\\\\\\'", '', comment)
comment = comment.replace('\\\\\\\\', "")
comment = comment.replace('\\\\\\\\\'', "")
comment = comment.replace('\\\\\'', "")
comment = comment.replace("\\\\", "")
我希望它看起来像这样......
It was quick and great ! And it's customized
在搞清楚之后,我遇到了两个主要问题。
我遇到的主要问题是PyCharm给了我错误的价值。实际值为It was quick and great ! And it\\'s customized
我没有将comment
的结果保存回review
,然后再附加到filtered_reviews
感谢大家的意见和帮助。
答案 0 :(得分:2)
你不需要正则表达式,在反斜杠后面查找撇号,只用撇号替换整个模式:
In [1]: s = "It was quick and great ! And it\\\\'s customized"
In [2]: s.replace(r"\\'", "'")
Out[2]: "It was quick and great ! And it's customized"
根据你的repr输出,你没有多个反斜杠你有2:
Out[4]: u"It was quick and great ! And it\\'s customized"
In [5]: s.replace(r"\'","'")
Out[5]: u"It was quick and great ! And it's customized"
答案 1 :(得分:0)
如果你想要试试正则表达式。
>>>import re
>>>re.sub(r'(\\\\)',r'',"""It was quick and great ! And it\\\\'s customized""")
>>>"It was quick and great ! And it's customized"