5

Suppose I have a mysterious unicode string in Python (2.7) that I want to feed to a command line program such as imagemagick (or really just get it out of Python in any way). The strings might be:

  • Adolfo López Mateos
  • Stanisława Walasiewicz
  • Jörgen Jönsson

So in Python I might make a little command like this:

cmd = u'convert -pointsize 24 label:"%s" "%s.png"' % (name, name)

If I just print cmd and get convert -pointsize 24 label:"Jörgen Jönsson" "Jörgen Jönsson.png" and then run it myself, everything is fine.

But if I do os.system( cmd ), I get this:

I know it's not an imagemagick problem because the filenames are messed up too. I know that Python is converting the command to ascii when it passes it off to os.system, but why is it getting the encoding so wrong? Why is it interpreting each non-ASCII character as 2 characters? According to a few articles that I've read, it might be because it's encoded as latin-1 but it's being read as utf-8, but I've tried encoding it back and forth between them and it's not helping.

I get Unicode exceptions when I try to just encode it manually as ascii without a replacement argument, but if I do name.encode('ascii','xmlcharrefreplace'), I get the following:

I'm hoping that someone recognizes this particular kind of encoding problem and can offer some advice, because I'm about out of ideas.

Thanks!

6
  • 1
    That's UTF-8 misinterpreted as Mac OS Roman, ASCII is not a player here at all. You should try os.system(cmd.encode("mac-roman")) Commented Jan 11, 2013 at 23:38
  • Thanks Esailija! So if I run my mystery string through name.encode('mac-roman', 'replace').decode('utf-8', 'replace') I get most of the way there, but it still barfs on Stanisława Walasiewicz. Any ideas? Commented Jan 11, 2013 at 23:52
  • just realized I used a nazi as an example name. I knew it sounded familiar :( Commented Jan 11, 2013 at 23:59
  • No I meant that you should use os.system(cmd.encode("mac-roman")) (cmd is unicode string)... there is no point in decoding and encoding right after Commented Jan 12, 2013 at 0:34
  • If you're looking for something imagemagick-specific, I'd use wand - dahlia.kr/wand Commented Jan 12, 2013 at 2:20

1 Answer 1

15

Use subprocess.call instead:

>>> s = u'Jörgen Jönsson'
>>> import subprocess
>>> subprocess.call(['echo', s])
Jörgen Jönsson
0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.