推荐学习书目
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 · May 6, 2018 · 2912 views
    This topic created in 2932 days ago, the information mentioned may be changed or developed.

    上面主要是想去掉^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  
       May 6, 2018
    你这是在处理 hive 导出的文件?
    https://blog.csdn.net/qq_16018407/article/details/78901809
    param
        2
    param  
       May 7, 2018 via Android
    Python 字符串有反斜槓的話,直接在字符串前加 r 吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2952 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 06:10 · PVG 14:10 · LAX 23:10 · JFK 02:10
    ♥ Do have faith in what you're doing.