简体   繁体   English

Python subprocess.check_output解析错误

[英]Python subprocess.check_output parsing error

Hopefully a simple problem, using subprocess.check_output I am trying to execute sqlite3 and read in the output. 希望是一个简单的问题,使用subprocess.check_output我试图执行sqlite3并读取输出。 There is a flag available for the sqlite3 CLI where you can initiate it with "-separator ','" to change the divider for output to a comma. sqlite3 CLI有一个可用的标志,您可以在其中用“ -separator','”将其启动,以将分隔符更改为输出到逗号。 It works at the command line, if I include it like this; 如果我这样包含它,它可以在命令行运行;

sqliteOutput = subprocess.check_output(["sqlite3 "," -separator ',' ",dbLocation,"SELECT blah from argh"])

Then it fails with an sqlite3 CLI error, "Error: too many options" and quotes the SELECT statement, but if I just run the command as above on a shell without the format for the subprocess command it works as expected. 然后,它因sqlite3 CLI错误"Error: too many options"而失败,并引用SELECT语句,但是如果我只是在没有子处理命令格式的shell上按上述方式运行命令,则该命令将按预期工作。

If I run it again without the separator it runs perfectly, like so; 如果我在没有分隔符的情况下再次运行它,它将运行得很好,就像这样;

sqliteOutput = subprocess.check_output(["sqlite3 ",dbLocation,"SELECT blah from argh"])

Obviously I am either mis-understanding how it is interpreting the separator argument in the subprocess, is there a way I can modify it to function correctly? 显然,我或者是误解了它是如何解释子进程中的分隔符参数的,是否可以修改它以使其正常运行? For the inquisitive, I cannot use the sqlite3 python library for this. 出于好奇,我不能为此使用sqlite3 python库。

here's a correction: 这是一个更正:

["sqlite3 ", " -separator ',' ", dbLocation, "SELECT blah from argh"] # yours
["sqlite3", "-separator", ",", dbLocation, "SELECT blah from argh"] # mine

the space in the beginning of " -separatator" is wrong. “ -separatator”开头的空格是错误的。

having "separator" and "," in one argument is wrong too. 在一个参数中包含“ separator”和“,”也是错误的。

space at the end of "sqlite3 " is wrong as well. “ sqlite3”末尾的空格也是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM