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

python 路径字符串替换问题

  •  
  •   fxxkgw · Sep 26, 2016 · 2748 views
    This topic created in 3524 days ago, the information mentioned may be changed or developed.
    i = '/export/server/xxx/bin/test'

    我要把'/'替换成'\/'

    i = '\/export\/server\/xxx\/bin\/test'

    但是用了 i.replace 和 re.sub 方法得到的都是
    \\/export\\/server\\/xxx\\/bin\\/test

    请问这个需求怎么实现呢?
    7 replies    2016-09-26 18:31:03 +08:00
    RIcter
        1
    RIcter  
       Sep 26, 2016
    你得 print ,不 print 怎么看都是 \\/
    raysonx
        2
    raysonx  
       Sep 26, 2016
    为什么要替换?楼主要手动 encode 和 decode ?
    fxxkgw
        3
    fxxkgw  
    OP
       Sep 26, 2016
    @RIcter 是的 用 print 就正常 但我需要获取这个值 用字符串的 format 格式也不行


    @raysonx 想获取值然后调用 shell 脚本执行 shell 里面的 sed 需要的是字符串 不是路径
    raysonx
        4
    raysonx  
       Sep 26, 2016
    @fxxkgw 非得调用 shell 脚本吗?手动处理很容易出现注入。
    littleshy
        5
    littleshy  
       Sep 26, 2016
    /又不需要转义,怎么可能出现\/这种东西……
    aiver
        6
    aiver  
       Sep 26, 2016
    @fxxkgw 你用 i.replace 后就是你要的格式了,里面出现\\V 是系统显示的效果,因为\需要转义,你用 print 就是实际的字符串了,不要纠结它在系统的表现形式
    imn1
        7
    imn1  
       Sep 26, 2016
    sed 用#做分隔符就不需要转义了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1090 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 18:18 · PVG 02:18 · LAX 11:18 · JFK 14:18
    ♥ Do have faith in what you're doing.