Sublime Forum

Weird ouput using Popen in ST3

#1

I’m developing a plugin for Sublime text and due some limitations of python in that environment I’m trying to get an json answer with Popen, I receive the data correctly, but seems like Popen is adding some weird characters in the output.

This is the ouput

(b'{"trink": {"id": "12"}}\r\n')

but it should be

{"trink": {"id": "12"}}

This is how I make the call:

process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True) output = process.communicate() print(output)

With

output = process.communicate()[0]

only disappears the () from the original output

How can I get an clean output?

I’m using Sublime text 3 Build 3083
Windows 10-64b

0 Likes

#2

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)
0 Likes

#3

Thanks FichteFoll, I google a lot about this “issue” before, and I didn’t fount anything about it.

I tried both options (decode and universal_newlines) and both showed me the result I wanted.

Thanks for your explanation again, as you say, I’m a beginner in this language.

0 Likes

#4

[quote=“gepd”]…]
Thanks for your explanation again, as you say, I’m a beginner in this language.[/quote]

Beginner or not, it’s always good to have an eye on the documentation. Here is the page which says Popen.communicate returns a tuple: 17.5. subprocess — Subprocess management ‑ Popen.communicate()

I’m not adding anything to what FichteFoll gave, just showing where to go.

Personally, I always had offline documentations, and since some weeks, I’m using Zeal on Linux. It’s very convenient to search for API documentation using this.

0 Likes

#5

+1 for using offline documentation browser and Zeal in particular, which is exactly where I extracted that information from. There is a package for that, of course: packagecontrol.io/packages/Zeal

0 Likes

#6

You suppose I don’t, I’ve been two days looking for a solution, the problem when you’re beginning is that the answer is not so obvious as how is for you, plus the documentation in my native language (spanish) is not so complete as in english.

Sorry if my question bother you.

0 Likes

#7

[quote=“gepd”]…]
Sorry if my question bother you.[/quote]

No it did not.

0 Likes