[英]Issue with special characters in Python subprocess
I translated this bash one-liner: 我翻译了这个bash单行代码:
awk '/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1' filename2 > filename1
into this Python code 进入此Python代码
with open('filename1', 'w') as f:
call(['awk', '/\\\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1', 'filename2'], stdout=f)
The output file, however, is empty, and it is not when I use the bash. 但是,输出文件为空,当我使用bash时不是。
With this: 有了这个:
call(['awk', r"'/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1'"], stdout=f)
I get 我懂了
awk: '/]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec;
awk:'/]:$ / {pno = NR; prec = $ 0; next} pno &&!(/ ^ I / && NR == pno + 1){print prec; pno=0} 1' awk: ^ invalid char ''' in expression
pno = 0} 1'awk:^表达式中的无效char'''
Sample Input file: 样本输入文件:
Interval: [ some_value some_value1]:
Interval: [ some_value some_value2]:
some text here1
some text here2
some text here3
some text here4
Interval: [ some_value some_value3]:
Interval: [ some_value some_value4]:
Interval: [ some_value some_value5]:
Interval: [ some_value some_value6]:
some text here5
some text here6
some text here7
some text here8
Interval: [ some_value some_value7]:
Interval: [ some_value some_value8]:
Sample Output File: 样本输出文件:
Interval: [ some_value some_value2]:
some text here1
some text here2
some text here3
some text here4
Interval: [ some_value some_value6]:
some text here5
some text here6
some text here7
some text here8
I bet you have an issue with your string conversion. 我敢打赌,您的字符串转换有问题。 Python version gives:
Python版本提供:
>>> print('/\\\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1')
/\\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1
While the shell version gives. 虽然外壳版本给。
$ echo '/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1'
/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1
You can simplify this kind of stuff using raw string notation: 您可以使用原始字符串表示法简化此类工作:
>>> print(r'/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1')
/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1
From the documentation: 从文档中:
When an
r
orR
prefix is present, backslashes are still used to quote the following character, but all backslashes are left in the string.当存在
r
或R
前缀时,反斜杠仍用于引用以下字符,但是所有反斜杠都保留在字符串中。 For example, the string literal r"\\n" consists of two characters: a backslash and a lowercase `n'.例如,字符串文字r“ \\ n”由两个字符组成:反斜杠和小写字母'n'。 String quotes can be escaped with a backslash, but the backslash remains in the string;
可以使用反斜杠对字符串引号进行转义,但反斜杠仍保留在字符串中; for example, r"\\"" is a valid string literal consisting of two characters: a backslash and a double quote
例如,r“ \\”“是由两个字符组成的有效字符串文字:反斜杠和双引号
So for your command: 因此,对于您的命令:
call(['awk', r'/\]:$/{pno=NR;prec=$0;next} pno && !(/^I/ && NR==pno+1){print prec; pno=0} 1', filename2], stdout=f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.