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

subprocess 中的 call 来执行 shell 命令,碰到转义字符怎么办呢?

  •  
  •   sjmcefc2 · 2018-05-06 15:35:27 +08:00 · 2394 次点击
    这是一个创建于 2181 天前的主题,其中的信息可能已经有所发展或是发生改变。

    上面主要是想去掉^A 前的.但是却失败了。 shell 中直接执行 sed -i 's/\^A/^A/g' test.txt 就能成功了

    感觉应该是表达式出了问题,可是引用 re 貌似也没效果,请教一下这个的正确写法。

               from subprocess import call
               import re
               fp = 'test.txt'
               call(["sed","-i","s/\\\^A/\^A/g",fp]) #ctrl+v,a
               call(["sed","-i",r"s/\\\^A/\^A/g",fp])
    

    或者不用 sed,用 python 能替换掉也好。

    longchisihai
        1
    longchisihai  
       2018-05-06 22:41:03 +08:00
    你这是在处理 hive 导出的文件?
    https://blog.csdn.net/qq_16018407/article/details/78901809
    param
        2
    param  
       2018-05-07 05:34:21 +08:00 via Android
    Python 字符串有反斜槓的話,直接在字符串前加 r 吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3414 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:35 · PVG 19:35 · LAX 04:35 · JFK 07:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.