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