0

I have the following string: s='T,Q0Z+QlT2ElT!A+Z,\u001B\u0003q' and would like to tell Python this is a Unicode string and have it converted to u'T,Q0Z+QlT2ElT!A+Z,\u001B\u0003q'

This does not work because the \ get escaped to \\:

In [29]: s = 'T,Q0Z+QlT2ElT!A+Z,\u001B\u0003q'

In [30]: u = unicode(s)

In [31]: u
Out[31]: u'T,Q0Z+QlT2ElT!A+Z,\\u001B\\u0003q'

Note s hasn't been prefixed with the u which is the source of the problem. My variable s is s = 'T,Q0Z+QlT2ElT!A+Z,\u001B\u0003q' and not s = u'T,Q0Z+QlT2ElT!A+Z,\u001B\u0003q'.

The result I want to get is u'T,Q0Z+QlT2ElT!A+Z,\u001B\u0003q' which is equal to u'T,Q0Z+QlT2ElT!A+Z,\x1B\x3q by the way.

Should be simple, but I'm struggling to find the solution. Thanks!

Edit: This is Python 2. I cannot add a u' before that string s because it comes to me that way... Obviously doing something like this s='u\'' +s won't work

user1381
  • 506
  • 1
  • 5
  • 19
  • Python 2 I guess. Try to put a `u` before it like you see in the output. Also make sure your source encoding is [declared](https://www.python.org/dev/peps/pep-0263/) in the first line. – Klaus D. Jul 10 '18 at 09:34
  • I can't change the string s. It comes to me that way. I've clarified the question. – user1381 Jul 10 '18 at 09:39
  • Thanks indeed for the duplicate mention with the answer I was looking for: `s.decode('unicode-escape')`. This solves my problem :) – user1381 Jul 10 '18 at 13:03

0 Answers0