如何对字符串进行反转义,如"\r\n"得到"\\r\\n",谢

2014-12-11 12:55:21 +08:00
 aheadlead
8126 次点击
所在节点    Python
8 条回复
tomriddle
2014-12-11 12:59:14 +08:00
import re
re.escape('\\r\\n')
iptux
2014-12-11 13:01:53 +08:00
```
$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '\r\n'.encode('string_escape')
'\\r\\n'
>>>
```

hex 的 encoding 也很好用
aheadlead
2014-12-11 13:05:14 +08:00
@iptux 成功了!感谢
banbanchs
2014-12-11 13:05:43 +08:00
s.encode("unicode_escape") ?
staticor
2014-12-11 13:05:52 +08:00
repr(s).replace('\\', '\\\\')
jiankangxin
2014-12-11 13:10:15 +08:00
@iptux 之前一直在用 replace 原来。。。
kungfuchicken
2014-12-11 13:12:20 +08:00
"\\".join(r'\r\n'.split("\\"))
lygmqkl
2014-12-11 15:18:24 +08:00
我都是在 js层做这个 转换得,感觉存入数据库得时候改变并不是很好。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/153100

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX