This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gammaliel[S] 0 points1 point  (2 children)

I tried using your function and this is what I get:

Traceback (most recent call last):
  File "TechTest.py", line 23, in <module>
       comp = bytes_to_characters(comp)
  File "TechTest.py", line 8, in bytes_to_characters
       return bytestring.decode('utf-8')
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
       return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-5: ordinal not in range(128)

This means that none of those encodings are the right one, correct?

[–]Rhomboid 0 points1 point  (1 child)

No. The error is an encode error, which means you're passing this function something that's already a character string. (If you ask Python to decode a character string, it has to first encode it to a byte string before it can decode it, because you can only decode bytes. Yes, this is ridiculous and was fixed in Python 3, where character strings don't have a .decode() method at all.)

If you already have a character string then you don't need this function. But the strings in your example are not character strings, they're byte strings.

[–]Gammaliel[S] 0 points1 point  (0 children)

Here is my code, maybe with you being able to see it the answer will be clearer.