Notice that the error is about encoding to ASCII, not about decoding from cp307. But you're not asking it to encode anywhere, so why is this happening?
Well, there are actually two possible places this could go wrong, and we can't know which of them it is without some help from you.
First, if your result[2]
is already a unicode
object, calling decode('cp037')
on it will first try to encode
it with sys.getdefaultencoding()
, which is usually 'ascii'
, so that it has something to decode. So, instead of getting an error saying "Hey, bozo, I'm already decoded", you get an error about encoding to ASCII failing. (This may seem very silly, but it's useful for a handful of codecs that can decode unicode
->unicode
or unicode
->str
, like ROT13 and quoted-printable.)
If this is your problem, the solution is to not call decode
. You've presumably already decoded the data somewhere along the way to this point, so don't try to do it again. (If you've decoded it wrong, you need to figure out where you decoded it and fix that to do it right; re-decoding it after it's already wrong won't help.)
Second, passing a Unicode string to print
will automatically try to encode
it with (depending on your Python version) either sys.getdefaultencoding()
or sys.stdout.encoding
. If Python has failed to guess the right encoding for your console (pretty common on Windows), or if you're redirecting your script's stdout to a file instead of printing to the console (which means Python can't possibly guess the right encoding), you can end up with 'ascii'
even in sys.stdout.encoding
.
If this is your problem, you have to explicitly specify the right encoding for your console (if you're lucky, it's in sys.stdout.encoding
), or the encoding you want for the text file you're redirecting to (probably 'utf-8'
, but that's up to you), and explicitly encode
everything you print
.
So, how do you know which one of these it is?
Simple. print type(result[2])
and see whether it's a unicode
or a str
. Or break it up into two pieces: x = result[2].decode('cp037')
and then print x
, and see which of the two raises. Or run in a debugger. You have all kinds of options for debugging this, but you have to do something.
Of course it's also possible that, once you fix the first one, you'll immediately run into the second one. But now you know how to deal with that to.
Also, note that cp037 is EBCDIC, one of the few encodings that Python knows about that isn't ASCII-compatible. In fact, '\xe3'
is EBCDIC for the letter T
.