【发布时间】:2016-07-14 19:22:34
【问题描述】:
我正在使用sh 在 Python 脚本中运行 git 命令。
In [1]: from sh import git
In [2]: s = git("log", "-1", pretty="format:%h %s")
In [3]: print s
4f14a66 basic debug page
这似乎按预期工作。但是,在 Django 模板中使用它会得到[?1h= 4f14a66 basic debug page[m [K[?1l>。我尝试使用repr() 查看此字符串中有哪些字符,但无济于事:
In [4]: print repr(s)
4f14a66 basic debug page
原来sh 中的命令返回一个具有.stdout 属性的RunningCommand:
In [5]: type(s)
Out[5]: sh.RunningCommand
In [7]: s.stdout
Out[7]: '\x1b[?1h\x1b=\r4f14a66 basic debug page\x1b[m\r\n\r\x1b[K\x1b[?1l\x1b>'
我如何获得"4f14a66 basic debug page" 即没有转义的字符串?从 Bash 运行命令很好:
$ git log -1 --pretty="format:%h %s"
4f14a66 basic debug page
【问题讨论】: