shlex.split 的反面是什么?

发布于 2024-10-13 04:27:39 字数 398 浏览 2 评论 0原文

如何反转 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如歌彻婉言 2024-10-20 04:27:39

我们现在(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 using pipes.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 to shlex and made officially public. See also http://bugs.python.org/issue1724822.

童话 2024-10-20 04:27:39

使用 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'"

How about using 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'"

.

划一舟意中人 2024-10-20 04:27:39

它是 python 3.8 中的 shlex.join()

It's shlex.join() in python 3.8

冬天旳寂寞 2024-10-20 04:27:39

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

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

请参阅 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 to shlex.quote(). In the bug report, a suggested implementation is mentioned:

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

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

听你说爱我 2024-10-20 04:27:39

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

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).

哭泣的笑容 2024-10-20 04:27:39

虽然 shlex.quote 在 Python 3.3 和 shlex.join 在 Python 3.8 中可用,它们并不总是服务作为 shlex.split 的真正“逆转”。观察以下代码片段:

import shlex
command = "cd /home && bash -c 'echo $HOME'"
print(shlex.split(command))
# ['cd', '/home', '&&', 'bash', '-c', 'echo $HOME']
print(shlex.join(shlex.split(command)))
# cd /home '&&' bash -c 'echo $HOME'

请注意,在拆分然后连接之后,&& 标记现在带有单引号。如果您现在尝试运行该命令,则会收到错误:cd:太多参数

如果您按照其他人的建议使用subprocess.list2cmdline(),效果会更好使用像 && 这样的 bash 运算符:

import subprocess
print(subprocess.list2cmdline(shlex.split(command)))
# cd /home && bash -c "echo $HOME"

但是您现在可能会注意到引号现在是双引号而不是单引号。这会导致 $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:

import shlex
command = "cd /home && bash -c 'echo $HOME'"
print(shlex.split(command))
# ['cd', '/home', '&&', 'bash', '-c', 'echo $HOME']
print(shlex.join(shlex.split(command)))
# cd /home '&&' bash -c 'echo $HOME'

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 &&:

import subprocess
print(subprocess.list2cmdline(shlex.split(command)))
# cd /home && bash -c "echo $HOME"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文