推荐学习书目
› 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
frogs
V2EX  ›  Python

问个关于raw string的问题

  •  
  •   frogs · Feb 18, 2013 · 4350 views
    This topic created in 4970 days ago, the information mentioned may be changed or developed.
    不知道有没有便捷的将string 转换成raw string的方法
    比如:将正则表达式"\d+(123)\s" 转换成 等同于r'\d+(123)\s'效果的raw string
    7 replies  •  1970-01-01 08:00:00 +08:00
    frogs
        1
    frogs  
    OP
       Feb 18, 2013
    额 人工置顶 跪求解答 m(_ _)m
    keakon
        2
    keakon  
       Feb 18, 2013   ❤️ 2
    >>> `"\d+(123)\s"`
    "'\\\\d+(123)\\\\s'"
    >>> repr("\d+(123)\s")
    "'\\\\d+(123)\\\\s'"

    这个?
    reorx
        3
    reorx  
       Feb 18, 2013
    不需要转换呀

    >>> '\d+(123)\s' is r'\d+(123)\s'
    True
    clowwindy
        4
    clowwindy  
       Feb 18, 2013   ❤️ 1
    >>> codecs.decode(r'a\n','string_escape')
    'a\n'
    >>> codecs.encode(r'a\n','string_escape')
    'a\\\\n'
    frogs
        5
    frogs  
    OP
       Feb 18, 2013
    @keakon 我sb了,本来想问的是怎么把 字符串里面正则字符变成普通字符,
    "\d+(123)\s" 变成 r"\\d\+\(123\)\\s"这种
    keakon
        6
    keakon  
       Feb 18, 2013
    >>> print re.escape('\d+(123)\s')
    \\d\+\(123\)\\s

    这个?
    frogs
        7
    frogs  
    OP
       Feb 18, 2013
    @keakon 非常感谢,我得去扒一扒escape的src了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2794 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:43 · PVG 22:43 · LAX 07:43 · JFK 10:43
    ♥ Do have faith in what you're doing.