在 ST3 中使用 Popen 的奇怪输出
Weird ouput using Popen in ST3
我正在为 Sublime text 开发一个插件,由于 python 在那个环境中的一些限制,我试图用 Popen 得到一个 json 答案,我正确地收到了数据,但是似乎 Popen 在输出中添加了一些奇怪的字符。
这是输出
(b'{"trink": {"id": "12"}}\r\n')
但应该是
{"trink": {"id": "12"}}
我是这样打电话的:
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True)
output = process.communicate()
print(output)
有
output = process.communicate()[0]
仅从原始输出中消失 ()
如何获得干净的输出?
我使用的是 Sublime text 3 Build 3083
Windows10-64b
ST论坛有用户回答我:
For starters, this is 100% a Python issue and has nothing to do with
Sublime Text. It's also not a limitation but a feature. Literally.
Firstly, communicate returns a tuple with (stdout_data, stderr_data).
You most likely only need the stdout part, so you slice that tuple
with [0]
.
Secondly, the data that communicate returns is, by default, a set of
bytes. Look that up if you want. Bytes are represented with the b
in
front of a literal string and can be converted to real strings using
the .decode() method on it, with a parameter for the charset. This
could be utf-8 or cp1252 or just the result of
locale.getpreferredencoding()
. If you used Popen with the
universal_newlines=True
argument, Python would do that conversion
for you automatically.
And finally, the string you have until now has a new line appended at
the end, because that's how whatever tool you are using made its
output. You have to strip it with .strip(), if it's a problem (since
it's json, most likely not).
So what do we get now?
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
output = process.communicate()[0]#.strip()
print(output)
我正在为 Sublime text 开发一个插件,由于 python 在那个环境中的一些限制,我试图用 Popen 得到一个 json 答案,我正确地收到了数据,但是似乎 Popen 在输出中添加了一些奇怪的字符。
这是输出
(b'{"trink": {"id": "12"}}\r\n')
但应该是
{"trink": {"id": "12"}}
我是这样打电话的:
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True)
output = process.communicate()
print(output)
有
output = process.communicate()[0]
仅从原始输出中消失 ()
如何获得干净的输出?
我使用的是 Sublime text 3 Build 3083 Windows10-64b
ST论坛有用户回答我:
For starters, this is 100% a Python issue and has nothing to do with Sublime Text. It's also not a limitation but a feature. Literally.
Firstly, communicate returns a tuple with (stdout_data, stderr_data). You most likely only need the stdout part, so you slice that tuple with
[0]
.Secondly, the data that communicate returns is, by default, a set of bytes. Look that up if you want. Bytes are represented with the
b
in front of a literal string and can be converted to real strings using the .decode() method on it, with a parameter for the charset. This could be utf-8 or cp1252 or just the result oflocale.getpreferredencoding()
. If you used Popen with theuniversal_newlines=True
argument, Python would do that conversion for you automatically.And finally, the string you have until now has a new line appended at the end, because that's how whatever tool you are using made its output. You have to strip it with .strip(), if it's a problem (since it's json, most likely not).
So what do we get now?
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True) output = process.communicate()[0]#.strip() print(output)