shlex.split 的反面是什么?
如何反转 shlex.split
< 的结果/a>?也就是说,我怎样才能获得一个“类似于 Unix shell 的字符串”,给定一个我希望引用的字符串列表
?
Update0
我找到了一个Python bug,并在此处提出了相应的功能请求。
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?
Update0
I've located a Python bug, and made corresponding feature requests here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我们现在(3.3)有一个 shlex.quote 函数。
pipes.quote
已移动并记录在案(使用pipes.quote
的代码仍然有效)。请参阅 http://bugs.python.org/issue9723 了解整个讨论。subprocess.list2cmdline
是不应使用的私有函数。不过,它可以转移到shlex
并正式公开。另请参阅http://bugs.python.org/issue1724822。We now (3.3) have a shlex.quote function. It’s none other that
pipes.quote
moved and documented (code usingpipes.quote
will still work). See http://bugs.python.org/issue9723 for the whole discussion.subprocess.list2cmdline
is a private function that should not be used. It could however be moved toshlex
and made officially public. See also http://bugs.python.org/issue1724822.使用 Pipes.quote 怎么样?
。
How about using
pipes.quote
?.
它是 python 3.8 中的 shlex.join()
It's shlex.join() in python 3.8
有一个添加
shlex.join()
的功能请求,它将完全按照您的要求进行操作。不过,到目前为止,似乎没有任何进展,主要是因为它大多只是转发到shlex.quote()
。在错误报告中,提到了建议的实现:请参阅 https://bugs.python.org/issue22454
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 toshlex.quote()
. In the bug report, a suggested implementation is mentioned:See https://bugs.python.org/issue22454
subprocess
使用subprocess.list2cmdline()
。它不是官方的公共 API,但在subprocess
文档中提到过,我认为使用它非常安全。它比 Pipes.open() 更复杂(无论好坏)。subprocess
usessubprocess.list2cmdline()
. It's not an official public API, but it's mentioned in thesubprocess
documentation and I think it's pretty safe to use. It's more sophisticated thanpipes.open()
(for better or worse).虽然 shlex.quote 在 Python 3.3 和 shlex.join 在 Python 3.8 中可用,它们并不总是服务作为
shlex.split
的真正“逆转”。观察以下代码片段:请注意,在拆分然后连接之后,
&&
标记现在带有单引号。如果您现在尝试运行该命令,则会收到错误:cd:太多参数
如果您按照其他人的建议使用
subprocess.list2cmdline()
,效果会更好使用像&&
这样的 bash 运算符:但是您现在可能会注意到引号现在是双引号而不是单引号。这会导致
$HOME
由 shell 展开,而不是像使用单引号一样逐字打印。总之,没有 100% 万无一失的撤销
shlex.split
的方法,您必须选择最适合您目的的选项,并注意边缘情况。While shlex.quote is available in Python 3.3 and shlex.join is available in Python 3.8, they will not always serve as a true "reversal" of
shlex.split
. Observe the following snippet:Notice that after splitting and then joining, the
&&
token now has single quotes around it. If you tried running the command now, you'd get an error:cd: too many arguments
If you use
subprocess.list2cmdline()
as others have suggested, it works nicer with bash operators like&&
:However you may notice now that the quotes are now double instead of single. This results in
$HOME
being expanded by the shell rather than being printed verbatim as if you had used single quotes.In conclusion, there is no 100% fool-proof way of undoing
shlex.split
, and you will have to choose the option that best suites your purpose and watch out for edge cases.