如何更换4个反斜杠

时间:2015-12-04 18:51:58

标签: python python-2.7

我有这个字符串,我从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)

commententer image description here

在这个特定的例子中,当我在PyCharm中逐步执行它时,变量commentreplace()执行后永远不会改变。我尝试过这些事情却没有成功......

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

更新

在搞清楚之后,我遇到了两个主要问题。

  1. 我遇到的主要问题是PyCharm给了我错误的价值。实际值为It was quick and great ! And it\\'s customized

  2. 我没有将comment的结果保存回review,然后再附加到filtered_reviews

  3. 感谢大家的意见和帮助。

2 个答案:

答案 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"