0

Motivation:

s =  诶

This is not an ASCII character, and we need to throw an error saying something like 'The string 诶 cannot be encoded using the encoding ASCII

But if we do something like,

raise UnicodeEncodeError('The string %s cannot be encoded using the encoding 
%s',%(s, encoding))

The above would reraise the error. but if we just do without specifying any encoding:

print(s)
诶

I guess this is because it just dumps the bytes to string.

Also something like this would raise error:

print ('{}'.format(str(s)))

and the below gives the unicode conversion:

 print('%s' ,%repr(s))
 u'\u8bf6'

is there a way to get the text which was the original text in the Error output.

Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
  • 2
    Possible duplicate of [How to read Unicode input and compare Unicode strings in Python?](https://stackoverflow.com/questions/477061/how-to-read-unicode-input-and-compare-unicode-strings-in-python) – Léopold Houdin Sep 27 '18 at 06:27

1 Answers1

0

I'm assuming this is Python 2.7?

Try this:

raise UnicodeEncodeError(u'The string %s cannot be encoded using the encoding 
                         %s' %(s, encoding))

The point being, that the u'...' literal is a unicode literal - so you can actually have unicode characters in it, without having to try to encode the unicode character to ascii.

Also note that when applying str(s), you are converting it back to ascii. So instead, do this:

print(u'{}'.format(s))

EDIT: The first example (UnicodeEncodeError) doesn't actually work, as @StephenRauch pointed out - I'm leaving it in here after fixing the syntax error...

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200