-1

I have this unicode

xx = u"Merci d'avoir acheté Electricité mobile, votre SPS sera activé prochainement"

when i print it it looks ok when i want to compare this xx the unicode appears like this

u'Merci d\'avoir acheté Electricité mobile, votre SPS sera activé prochainement'

i want to remove this backslash i tried like this but get exception

ddd=xx.decode('string_escape')

EncodeError: 'ascii' codec can't encode character u'\xe9' in position 19: ordinal not in range(128)

any help will be appriciated

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
ohadshay
  • 225
  • 1
  • 3
  • 16
  • 1
    The slash is only the representation. It is not really in the string. It is not even a non-ascii character, it is only the normal ascii single quote, the reason it is shown escaped by the backslash is because the string is also delimited with single quotes. – Paulo Scardine Nov 28 '17 at 19:12
  • It's actually a single slash, not sure what Pavel was trying to do when he changed it to a double slash. The single slash is there to escape the single quote. – AsheKetchum Nov 28 '17 at 19:15
  • You need to escape the single quote because if you had something like `'hi' there'` python sees the second single quote after the high and thinks that the string ends there. So you need to 'escape' the second quote so that python includes it in the string and does not terminate the string there. – AsheKetchum Nov 28 '17 at 19:17

2 Answers2

0

How about trying 'unicode_escape'

Aswini
  • 46
  • 4
0

If it's unicode, you should decode it this way:

xx.decode('UTF-8')

But str.decode() defaults to 'UTF-8' so you actually could leave out the argument

Sim Son
  • 310
  • 1
  • 10