推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
aheadlead
V2EX  ›  Python

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

  •  
  •   aheadlead · Dec 11, 2014 · 9369 views
    This topic created in 4349 days ago, the information mentioned may be changed or developed.
    8 replies  •  2014-12-11 15:18:24 +08:00
    tomriddle
    tomriddle
    Dec 11, 2014❤️1
    1
    import re
    re.escape('\\r\\n')
    iptux
    iptux
    Dec 11, 2014❤️2
    2
    ```
    $ 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
    aheadlead
    OP
    Dec 11, 2014❤️1
    3
    @iptux 成功了!感谢
    banbanchs
    banbanchs
    Dec 11, 2014
    4
    s.encode("unicode_escape") ?
    staticor
    staticor
    Dec 11, 2014
    5
    repr(s).replace('\\', '\\\\')
    jiankangxin
    jiankangxin
    Dec 11, 2014
    6
    @iptux 之前一直在用 replace 原来。。。
    kungfuchicken
    kungfuchicken
    Dec 11, 2014
    7
    "\\".join(r'\r\n'.split("\\"))
    lygmqkl
    lygmqkl
    Dec 11, 2014
    8
    我都是在 js层做这个 转换得,感觉存入数据库得时候改变并不是很好。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   684 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:41 · PVG 04:41 · LAX 12:41 · JFK 15:41
    ♥ Do have faith in what you're doing.