pomber
pomber

Reputation: 23980

String to unicode string

I've been reading some articles about unicode (this and this), but still don't understand how to do something as simple as this (in python 2.7):

From

x = 'm\\u00e1s'

I want to obtain the unicode string u'm\u00e1s' ("más").

What is the right way to do it?

Upvotes: 1

Views: 133

Answers (2)

Robᵩ
Robᵩ

Reputation: 168596

The literal answer to your question is to decode your original string using the raw_unicode_escape codec.

In [2]: x = 'm\\u00e1s'

In [3]: x.decode('raw_unicode_escape')
Out[3]: u'm\xe1s'

But that's almost certainly the wrong way to do whatever you are actually trying to do.

Upvotes: 2

reagle
reagle

Reputation: 1

x = 'm\\u00e1s'
unicode(x)

gives:

u'm\\u00e1s'

Upvotes: -1

Related Questions