![](/img/trans.png)
[英]Using Sed through subprocess.call in python to conduct in file replacements
[英]Sed with Python/subprocess.call: How can I make python execute this call to sed
我从外壳运行的sed是:
sed -re 's/(::\s+ni\s+=)[^=]*$/\1 512/' test.dat
但是,我无法使其与Python的subprocess.call一起运行:
我有以下几点:
infile = 'test.dat'
cmd= [
"sed",
"-re",
"s/(::\s+ni\s+=)[^=]*$/\1 512/",
infile
]
subprocess.call(cmd, stdout=open('out_test.dat','w'))
我尝试了许多不同的方法,但是我总是得到非零的退出状态。
问题在于Python字符串"s/(::\\s+ni\\s+=)[^=]*$/\\1 512/"
包含一个control-A,您需要一个反斜杠和一个1。要将正则表达式写为字符串文字,如果可能的话,您想使用原始字符串,否则请使用反斜杠。 因此,只需将该行更改为:
r"s/(::\s+ni\s+=)[^=]*$/\1 512/",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.