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

各位高手请看看怎么规避 Python2 中诡异的字符转义?

  •  
  •   vtoexsir · Dec 4, 2016 · 4487 views
    This topic created in 3624 days ago, the information mentioned may be changed or developed.

    pth='D:\mytemp\lunwenfcache\convert\20160907' #该路径明明存在
    print os.path.isdir(pth) #=>False #但这里却提示该路径不存在?
    后来,发现实际上 pth 字符串当中,'\201'实际上被转义了,所以第二句代码就返回 False.
    如果这样定义 pth 就不会出错:
    pth=r'D:\mytemp\lunwenfcache\convert\20160907'
    或者这样定义也不会出错:
    pth='D:\mytemp\lunwenfcache\convert\20160907'
    然而,在使用 python 的过程中,类似这样的字符串被悄悄转义的情况,表面上看起来似乎难以发觉.
    不知高手有什么好的经验? 多谢您的回复!

    17 replies  •  2016-12-05 22:33:11 +08:00
    thekoc
    thekoc
    Dec 4, 2016
    1
    字符串里用反斜杠就会被转义,如果完全不想被转义就用三个引号。另外可以用 os.path.join 函数来构造地址
    imn1
    imn1
    Dec 4, 2016
    2
    我只知道 py3 写成这样 D:/mytemp/lunwenfcache/convert/20160907 也行
    不确定的最好用三引号
    freestyle
    freestyle
    Dec 4, 2016
    3
    路径请用 / 无论 windows 还是 linux/macos 都支持
    anonymalias
    anonymalias
    Dec 4, 2016
    4
    可以加上个 r 、含义是: raw ,原始字符串,例如: pth= r'D:\mytemp\lunwenfcache\convert\20160907'
    正如楼上所说,路径要用正斜线, windows 也是支持的, windows 当初在 dos 时代用反斜线就是一大败笔。。。
    aristotll
    aristotll
    Dec 4, 2016
    5
    还可以考虑使用平台无关的 pathsep `os.path.sep`
    binux
    binux
    Dec 4, 2016
    6
    这是你不会编程的原因,这个锅 Python2 不背。
    cabbage
    cabbage
    Dec 4, 2016 via Android
    7
    Python2 普通字符串加 r 我是养成习惯的~
    omg21
    omg21
    Dec 4, 2016
    8
    @anonymalias 可以加上个 r 、含义是: raw ,原始字符串,例如: pth= r'D:\mytemp\lunwenfcache\convert\20160907'

    如果 D:\mytemp\lunwenfcache\convert\20160907 是自动获取的怎么加 r ?
    Geoion
    Geoion
    Dec 4, 2016
    9
    os.path.join 在写路径的时候确实可以规避很多问题
    kaneg
    kaneg
    Dec 4, 2016 via iPhone
    10
    除了楼上各位的建议外,请用一款合适的 IDE ,这类低级错误都会给你提示的
    anonymalias
    anonymalias
    Dec 4, 2016
    11
    @omg21 源是错的,没法加,
    terence4444
    terence4444
    Dec 4, 2016
    12
    我是这么写的,纯粹瞎搞:"D:\\mytemp\\lunwenfcache\\convert\\20160907"
    aaronzjw
    aaronzjw
    Dec 4, 2016
    13
    不要为了偷懒而不写 os.path.join()
    vtoexsir
    vtoexsir
    OP
    Dec 5, 2016
    14
    @thekoc 三引号包裹的字符串,如果包含\,也是会被转义的:
    pth='D:\mytemp\lunwenfcache\convert\20160907'
    pth_3='''D:\mytemp\lunwenfcache\convert\20160907'''
    print pth==pth_3 #=>True
    print os.path.isdir(pth_3) #=>False
    aliipay
    aliipay
    Dec 5, 2016
    15
    @binux 同意, pth 的缩写也是醉人
    rogerchen
    rogerchen
    Dec 5, 2016
    16
    @omg21 你拿不到你说的形式,要么就是被转义了,要么就是对的。
    ryd994
    ryd994
    Dec 5, 2016
    17
    @omg21 只有字面量才被转义,为了能在源码里写进不可打印字符
    内存里的数据没有转义,就是数据而已嘛
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1758 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 74ms · UTC 05:54 · PVG 13:54 · LAX 21:54 · JFK 00:54
    ♥ Do have faith in what you're doing.