什么是shlex.split的反面?

时间:2021-08-13 03:51:45

How can I reverse the results of a shlex.split? That is, how can I obtain a quoted string that would "resemble that of a Unix shell", given a list of strings I wish quoted?

如何反转shlex.split的结果?也就是说,如果给出一个我希望引用的字符串列表,我如何获得一个“类似于Unix shell”的带引号的字符串?

Update0

I've located a Python bug, and made corresponding feature requests here.

我找到了一个Python bug,并在此处提出了相应的功能请求。

5 个解决方案

#1


22  

We now (3.3) have a shlex.quote function. It’s none other that pipes.quote moved and documented (code using pipes.quote will still work). See http://bugs.python.org/issue9723 for the whole discussion.

我们现在(3.3)有一个shlex.quote函数。 pipes.quote移动并记录(使用pipes.quote的代码仍然可以工作)。有关整个讨论,请参见http://bugs.python.org/issue9723。

subprocess.list2cmdline is a private function that should not be used. It could however be moved to shlex and made officially public. See also http://bugs.python.org/issue1724822.

subprocess.list2cmdline是不应使用的私有函数。然而,它可以转移到shlex并正式公开。另见http://bugs.python.org/issue1724822。

#2


19  

How about using pipes.quote?

如何使用pipes.quote?

import pipes
strings = ["ls", "/etc/services", "file with spaces"]
" ".join(pipes.quote(s) for s in strings)
# "ls /etc/services 'file with spaces'"

.

#3


6  

subprocess uses subprocess.list2cmdline(). It's not an official public API, but it's mentioned in the subprocess documentation and I think it's pretty safe to use. It's more sophisticated than pipes.open() (for better or worse).

subprocess使用subprocess.list2cmdline()。它不是官方的公共API,但它在子流程文档中提到过,我认为使用起来非常安全。它比pipes.open()更复杂(无论好坏)。

#4


2  

Both 'foo' and "'foo'" are transformed by shlex.split to the same list:

'foo'和''foo'“都被shlex.split转换为相同的列表:

In [44]: shlex.split("'foo'")
Out[44]: ['foo']

In [45]: shlex.split("foo")
Out[45]: ['foo']

So I don't think it is possible to reverse shlex.split in all cases, but this might get you close:

所以我认为在所有情况下都不可能撤销shlex.split,但这可能会让你接近:

In [20]: import subprocess
In [21]: subprocess.list2cmdline(shlex.split('prog -s "foo bar"'))
Out[21]: 'prog -s "foo bar"'

In [22]: subprocess.list2cmdline(shlex.split('prog -s "foo bar" "baz"'))
Out[22]: 'prog -s "foo bar" baz'

#5


1  

There is a feature request for adding shlex.join(), which would do exactly what you ask. As of now, there does not seem any progress on it, though, mostly as it would mostly just forward to shlex.quote(). In the bug report, a suggested implementation is mentioned:

有一个添加shlex.join()的功能请求,它会完全按照您的要求执行。截至目前,它似乎没有任何进展,但主要是因为它主要只是转发到shlex.quote()。在错误报告中,提到了一个建议的实现:

' '.join(shlex.quote(x) for x in split_command)

See https://bugs.python.org/issue22454

请参阅https://bugs.python.org/issue22454

#1


22  

We now (3.3) have a shlex.quote function. It’s none other that pipes.quote moved and documented (code using pipes.quote will still work). See http://bugs.python.org/issue9723 for the whole discussion.

我们现在(3.3)有一个shlex.quote函数。 pipes.quote移动并记录(使用pipes.quote的代码仍然可以工作)。有关整个讨论,请参见http://bugs.python.org/issue9723。

subprocess.list2cmdline is a private function that should not be used. It could however be moved to shlex and made officially public. See also http://bugs.python.org/issue1724822.

subprocess.list2cmdline是不应使用的私有函数。然而,它可以转移到shlex并正式公开。另见http://bugs.python.org/issue1724822。

#2


19  

How about using pipes.quote?

如何使用pipes.quote?

import pipes
strings = ["ls", "/etc/services", "file with spaces"]
" ".join(pipes.quote(s) for s in strings)
# "ls /etc/services 'file with spaces'"

.

#3


6  

subprocess uses subprocess.list2cmdline(). It's not an official public API, but it's mentioned in the subprocess documentation and I think it's pretty safe to use. It's more sophisticated than pipes.open() (for better or worse).

subprocess使用subprocess.list2cmdline()。它不是官方的公共API,但它在子流程文档中提到过,我认为使用起来非常安全。它比pipes.open()更复杂(无论好坏)。

#4


2  

Both 'foo' and "'foo'" are transformed by shlex.split to the same list:

'foo'和''foo'“都被shlex.split转换为相同的列表:

In [44]: shlex.split("'foo'")
Out[44]: ['foo']

In [45]: shlex.split("foo")
Out[45]: ['foo']

So I don't think it is possible to reverse shlex.split in all cases, but this might get you close:

所以我认为在所有情况下都不可能撤销shlex.split,但这可能会让你接近:

In [20]: import subprocess
In [21]: subprocess.list2cmdline(shlex.split('prog -s "foo bar"'))
Out[21]: 'prog -s "foo bar"'

In [22]: subprocess.list2cmdline(shlex.split('prog -s "foo bar" "baz"'))
Out[22]: 'prog -s "foo bar" baz'

#5


1  

There is a feature request for adding shlex.join(), which would do exactly what you ask. As of now, there does not seem any progress on it, though, mostly as it would mostly just forward to shlex.quote(). In the bug report, a suggested implementation is mentioned:

有一个添加shlex.join()的功能请求,它会完全按照您的要求执行。截至目前,它似乎没有任何进展,但主要是因为它主要只是转发到shlex.quote()。在错误报告中,提到了一个建议的实现:

' '.join(shlex.quote(x) for x in split_command)

See https://bugs.python.org/issue22454

请参阅https://bugs.python.org/issue22454