为什么 raw 的 r 禁止转义会失效呢 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
eccentric579
V2EX    Python

为什么 raw 的 r 禁止转义会失效呢

  •  
  •   eccentric579 2021-11-06 16:07:28 +08:00 3220 次点击
    这是一个创建于 1435 天前的主题,其中的信息可能已经有所发展或是发生改变。
     aim = { "official" : r"D:\下载\Temp\official\", "others" : r"D:\下载\Temp\others\" } 

    屏幕截图 2021-11-06 160451.png

    8 条回复    2021-11-08 14:12:38 +08:00
    Dvel
        1
    Dvel  
       2021-11-06 16:26:09 +08:00
    原生字符串的结尾不能是 “反斜杠” + “引号” 。
    可以写成 r'D:\下载\Temp\official' + '\\'
    festoney8
        2
    festoney8  
       2021-11-06 16:26:55 +08:00
    leoleoasd
        3
    leoleoasd  
       2021-11-06 16:41:40 +08:00
    根据 python 文档:Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the folowing quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.
    leoleoasd
        4
    leoleoasd  
       2021-11-06 16:41:49 +08:00
    Trim21
        5
    Trim21  
       2021-11-06 16:43:36 +08:00 via Android
    只有最后一个反斜杠会失效
    eccentric579
        6
    eccentric579  
    OP
       2021-11-06 16:48:20 +08:00
    谢谢楼上的各位
    2i2Re2PLMaDnghL
        7
    2i2Re2PLMaDnghL  
       2021-11-06 23:29:30 +08:00
    这是为了让 syntax 简单,把 raw 放到 semantic 层来实现。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2474 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 01:38 PVG 09:38 LAX 18:38 JFK 21:38
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86