使用字符串我指定一个变量,例如:错误而蟒蛇
cmd="sed -i 's/id="556"/id="33442233"\n pss="120"/g' a.xml"
但是,当我尝试打印CMD即时得到一个语法错误,似乎是因为 多个'
和"
的...我该如何解决这个问题?
使用字符串我指定一个变量,例如:错误而蟒蛇
cmd="sed -i 's/id="556"/id="33442233"\n pss="120"/g' a.xml"
但是,当我尝试打印CMD即时得到一个语法错误,似乎是因为 多个'
和"
的...我该如何解决这个问题?
使用\
转义引号?
cmd="sed -i 's/id=\"556\"/id=\"33442233\"\n pss=\"120\"/g' a.xml"
使用三重引号:
cmd = '''sed -i 's/id="556"/id="33442233"\n pss="120"/g' a.xml'''
顺便说一句。我看到\n
有 - 如果你想保持它作为反斜线-N,而不是把它转换为一个换行符,你甚至可以用这样的:
cmd = r'''sed -i 's/id="556"/id="33442233"\n pss="120"/g' a.xml'''
工作对我来说:
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> cmd="sed -i \'s\/id=\"556\"\/id=\"33442233\"\n pss=\"120\"\/g\' a.xml"
>>> print cmd
sed -i 's\/id="556"\/id="33442233"
pss="120"\/g' a.xml
为了避免引号,在字符前加上反斜杠\
。例如:
"\"hello\" said he."
和'\'goodbye\' said she'