Subprocess.Popen (..). chat (..) throw data randomly when used with graphviz!

I am using graphviz dot to generate some svg graphs for a web application. I call the point using Popen:

p = subprocess.Popen(u'/usr/bin/dot -Kfdp -Tsvg', shell=True,\ stdin=subprocess.PIPE, stdout=subprocess.PIPE) str = u'long-unicode-string-i-want-to-convert' (stdout,stderr) = p.communicate(str) 

What happens is that the point program throws errors, for example:

  Error: not well-formed (invalid token) in line 1 ... <tr><td cellpadding="4bgcolor="#EEE8AA"> ... in label of node n260 

This obvious error is most likely NOT in the input line. In particular, if I save it in str.txt with utf-8 encoding and do

 /usr/bin/dot -Kfdp -Tsvg < str.txt > myimg.svg 

I get the desired result. The only “special” thing about str is that it contains characters such as Danish øæå.

Now I have no idea what to do. The problem can be very successful; but, apparently, this is apparently due to the fact that Popen is different from using <from the shell, and I have no idea where to start. Any help or ideas for an alternative point call (besides writing all the data to a file and calling this!) Would be greatly appreciated!

+4
source share
1 answer

It looks like you should do:

 stdout, stderr = p.communicate(str.encode('utf-8')) 

(except that you should not shade embedded str .) The Unicode type in Python contains Unicode data, not UTF-8. If you want UTF-8, you need to explicitly encode it.

In addition, it makes no sense to use shell=True in this fragment, as well as the unicode literal passed to the subprocess. Come up with a particularly good idea (it is still encoded in ASCII). And a backslash at the end is not required - Python knows that the line continues because you have an open bracket that is not yet closed. So use:

 p = subprocess.Popen(['/usr/bin/dot', '-Kfdp', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) 
+3
source

Source: https://habr.com/ru/post/1301062/


All Articles