shlex.split的反面是什么?

2024-05-19 10:28:56 发布

您现在位置:Python中文网/ 问答频道 /正文

如何反转^{}的结果?也就是说,如果给定要引用的字符串的list,我如何获得将"resemble that of a Unix shell"引用的字符串?

更新0

我找到了一个Python错误,并提出了相应的功能请求here


Tags: of字符串功能thathere错误unixshell
3条回答

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

是的。

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

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

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

相关问题 更多 >

    热门问题