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
YaphetYin
V2EX  ›  Python

怎么在 python 中单独输出反斜杠\为字符串?

  •  
  •   YaphetYin ·
    filosfino · 2015-11-07 01:59:29 +08:00 · 22431 次点击
    这是一个创建于 3065 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前一阵有看到这个问题,可惜找不到了,就再来求安利一下~


    情况是这样的,有个文件文件名为'\u5feb\u901f\u4e0a\u4f20'

    然后我用os.listdir('.')显示的时候它自动给反斜杠转义了,就成了'\\u5feb\\u901f\\u4e0a\\u4f20'

    那么问题来了,怎样把双斜杠替换为单斜杠?

    我尝试用字符串的 replace ,可是a.replace('\\', '\')会因为反斜杠把单引号转义了而找不到字符串结束符报错,用a.replace(r'\\', r'\')也不行。

    有什么好方法可以助我目标达成吗?

    第 1 条附言  ·  2015-11-07 15:16:24 +08:00
    name = '\\u5feb' 
    name[0] = '\\'
    

    也就是说'\\u5feb'中第一个反斜杠不是字符串的一部分,而是用于转义第二个反斜杠的。

    所以一开始我的思路就不对了,不应该用 replace 去找第一个反斜杠因为它根本就不存在,而应该用 decode('unicode_escape') 来解决。

    13 条回复    2015-11-07 13:51:48 +08:00
    RickyBoy
        1
    RickyBoy  
       2015-11-07 03:53:00 +08:00
    decode('unicode_escape') 直接显示中文不好么
    RickyBoy
        2
    RickyBoy  
       2015-11-07 05:06:12 +08:00
    接楼上,如果文件名本身就是 '\u5feb\u901f\u4e0a\u4f20' 的话, os.listdir 输出的列表肯定是 '\\u5feb\\u901f\\u4e0a\\u4f20' 的形式的, for 一下单独显示字符串的话就没有了
    Sylv
        3
    Sylv  
       2015-11-07 06:52:24 +08:00 via iPhone
    >>> name = '\u5feb\u901f\u4e0a\u4f20'
    >>> print name
    \u5feb\u901f\u4e0a\u4f20
    >>> print repr(name)
    '\\u5feb\\u901f\\u4e0a\\u4f20'
    >>> print [name]
    ['\\u5feb\\u901f\\u4e0a\\u4f20']
    >>> print [name][0]
    \u5feb\u901f\u4e0a\u4f20
    binux
        4
    binux  
       2015-11-07 07:04:25 +08:00   ❤️ 1
    这是一个 XY 问题
    jimzhong
        5
    jimzhong  
       2015-11-07 08:12:20 +08:00
    为什么要转啊?你看到的是转义后的字符串。
    des
        6
    des  
       2015-11-07 08:19:47 +08:00
    3.4

    >>> name = '\u5feb\u901f\u4e0a\u4f20'
    >>>
    >>> print name
    SyntaxError: Missing parentheses in call to 'print'
    >>> print(name)
    快速上传
    p1n3
        7
    p1n3  
       2015-11-07 11:00:09 +08:00
    python2.7

    >>> print u'\u5feb\u901f\u4e0a\u4f20'
    快速上传
    >>> print '\\'
    \
    Cu635
        8
    Cu635  
       2015-11-07 11:04:28 +08:00
    @des python3 里面 print 必须加括号了。不能直接“ print xxx ”了。
    Kisesy
        9
    Kisesy  
       2015-11-07 12:50:14 +08:00
    其实在程序内部是单斜杠,只是给你显示为双斜杠
    YaphetYin
        10
    YaphetYin  
    OP
       2015-11-07 12:57:38 +08:00
    @RickyBoy `decode('unicode_escape')`是正解,今天试了一下成功了
    YaphetYin
        11
    YaphetYin  
    OP
       2015-11-07 13:09:48 +08:00
    @p1n3
    @Kisesy
    @jimzhong
    @binux

    Bingo!

    其实 listdir 出来的字符串'\\u5feb'就是'\u5feb',这么说可能有些 confusing ,这样可能容易理解一点:

    name = '\\u5feb'
    name[0] = '\\'

    也就是说'\\u5feb'中第一个反斜杠不是字符串的一部分,而是用于转义第二个反斜杠的。所以大一开始我的思路就不对了,应该用 decode('unicode_escape') 来解决。
    overvenus
        12
    overvenus  
       2015-11-07 13:24:58 +08:00
    ➜ ~ python3
    Python 3.4.3 (default, Oct 14 2015, 20:28:29)
    [GCC 4.8.4] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> name = """\u5feb\u901f\u4e0a\u4f20"""
    >>> print(name)
    快速上传
    >>> print(repr(name))
    '快速上传'
    >>> name = r"""\u5feb\u901f\u4e0a\u4f20"""
    >>> print(name)
    \u5feb\u901f\u4e0a\u4f20
    >>>
    fyyz
        13
    fyyz  
       2015-11-07 13:51:48 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3236 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 00:39 · PVG 08:39 · LAX 17:39 · JFK 20:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.