我有一个 python 脚本,它从命令行参数中获取输入。例如:
./myscript.py first_item second\ item "third item"
我可以使用 pipes.quote
输出单独的项目、转义空格和特殊字符.
print " ".join(map(pipes.quote, outputItems))
是否有任何现有的“unquote
”接口(interface)可以解析 bash 参数字符串,同时保持转义空格和引号字符串完整?
允许同一个 python 脚本处理这个的东西:
echo 'first_item second\ item "third item"' | ./myscript.py
最佳答案
你想要shlex.split()
:
s = 'first_item second\ item "third item"'
import shlex
shlex.split(s)
Out[3]: ['first_item', 'second item', 'third item']
关于python - "Unquote"/在python中解析bash参数字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22351646/