添加反斜杠而不转义[Python] [英] Adding backslashes without escaping [Python]

查看:449
本文介绍了添加反斜杠而不转义[Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在字符串中转义& (&符号)字符。问题是每当我 string = string.replace('&','\&')结果是'\\ &安培;'。添加一个额外的反斜杠来转义原来的反斜杠。

I need to escape a & (ampersand) character in a string. The problem is whenever I string = string.replace ('&', '\&') the result is '\\&'. An extra backslash is added to escape the original backslash. How do I remove this extra backslash?

推荐答案

结果'\\&'仅显示 - 实际上字符串是 \&

The result '\\&' is only displayed - actually the string is \&:

>>> str = '&'
>>> new_str = str.replace('&', '\&')
>>> new_str
'\\&'
>>> print new_str
\&

在shell中尝试。

这篇关于添加反斜杠而不转义[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆