How to add unicode character before string? [Python]

I want to be able to add 'u' to a reference string variable. I need to do this because when I am in the for loop, I can only access the string using the variable name.

Is there any way to do this?

>>> word = 'blahblah' >>> list = ['blahblah', 'boy', 'cool'] >>> import marisa_trie >>> trie = marisa_trie.Trie(list) >>> word in trie Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Argument 'key' has incorrect type (expected unicode, got str) >>> 'blahblah' in trie Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Argument 'key' has incorrect type (expected unicode, got str) >>> u'blahblah' in trie True >>> u"blahblah" in trie True >>> u(word) in trie Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'u' is not defined >>> uword in trie Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'uword' is not defined >>> u+word in trie Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'u' is not defined >>> word.u in trie Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute 'u' 
+5
source share
4 answers

You can decode:

 lst = ['blahblah', 'boy', 'cool'] for word in lst: print(type(word.decode("utf-8"))) 

Or use the unicode function:

 unicode(word,encoding="utf-8")) 

Or str.format:

 for word in lst: print(type(u"{}".format(word))) 
+8
source

unicode(your_string) does exactly what you need, I believe.

 >>> unicode("Hello world"!) u"Hello world!" >>> print (unicode("Hello world"!)) "Hello world!" 
+2
source

Yes, format () will work, but sometimes it won’t. Older versions of Python do not even have this. I recommend:

 utext = u"%s" % text 

Which will do the same as unicode.format () if you don't like using the unicode () function. But obviously this is so .: D

+1
source

The u prefix can only be used for literals. To convert an existing string to a unicode object, use the unicode() constructor.

0
source

All Articles