Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:使用xmllint时,子进程通信将不确定地等待_Python_Subprocess_Python 2.6_Xmllint - Fatal编程技术网

python:使用xmllint时,子进程通信将不确定地等待

python:使用xmllint时,子进程通信将不确定地等待,python,subprocess,python-2.6,xmllint,Python,Subprocess,Python 2.6,Xmllint,我得到了Python2.6,它有一个旧版本的toprettyxml(),它没有按预期进行xml格式化。因此,我尝试使用子流程调用xmllint。这是我的简化代码 xmlParseCmd = "xmllint -format - <<< '%s'" % '<?xml version="1.0" encoding="UTF-8"?> <insertion> <mytag>123456</mytag> <mytag2&

我得到了Python2.6,它有一个旧版本的toprettyxml(),它没有按预期进行xml格式化。因此,我尝试使用子流程调用xmllint。这是我的简化代码

      xmlParseCmd = "xmllint -format - <<< '%s'" % '<?xml version="1.0" encoding="UTF-8"?> <insertion> <mytag>123456</mytag> <mytag2>789</mytag2> </insertion>'
      print shlex.split(xmlParseCmd)
      pxmlParser = subprocess.Popen(shlex.split(xmlParseCmd), stdout=subprocess.PIPE)
      pretty_xml = pxmlParser.communicate()[0]
      print pretty_xml

xmlpassecmd=“xmllint-format-如果您真的想
检查输出,请不要使用这里的字符串
Popen()
(或者使用现代Python 3.6+只需
run()
)即使你卡在Python 2上,你也确实应该考虑移动到2.7。对于新的开发,你真的很想把Python 3作为目标。Py2计划在明年结束,虽然它被放在额外的几年的终端护理中。下面是你为什么要避免<代码> shell=Trime< /Cord>。但目前我只能满足于2.6。除了传递
Popen(cmd,input=“string”)之外,还有其他方法可以用python 2.6实现这一点吗
或使用
shell=True
如此答案所示…?为什么不能使用其中任何一个?扩展了答案的这一部分;可能以前不清楚。感谢更新。
Popen(“xmllint-format-
 -> python ~/myscripts/resources/test_xtract.py
['xmllint', '-format', '-', '<<<', '<?xml version="1.0" encoding="UTF-8"?> <insertion> <mytag>123456</mytag> <mytag2>789</mytag2> </insertion>']
from subprocess import run, PIPE

xml = '<?xml version="1.0" encoding="UTF-8"?> <insertion> <mytag>123456</mytag> <mytag2>789</mytag2> </insertion>'
xmllint = run(['xmllint', '-format', '-'], input=xml, stdout=PIPE, universal_newlines=True)
print(xmllint.stdout)