简体   繁体   中英

How to add unicode character before a string? [Python]

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

Is there a 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'

You could 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)))

unicode(your_string) does just what you need, i believe.

>>> unicode("Hello world"!)
u"Hello world!"
>>> print (unicode("Hello world"!))
"Hello world!"

Yes, format() will work, but sometimes will not. Older versions of Python even doesn't have it. I recommend:

utext = u"%s" % text

Which will do the same thing, as unicode.format() If you don't like to use unicode() function. But obviously, you do. :D

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM