如何在字符串前添加unicode字符? [蟒蛇]

时间:2015-07-08 23:31:39

标签: python unicode

我希望能够在引用的字符串变量中添加“u”。我需要这样做,因为当我在for循环中时,我只能通过变量名访问该字符串。

有办法做到这一点吗?

>>> 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'

4 个答案:

答案 0 :(得分:10)

你可以解码:

lst = ['blahblah', 'boy', 'cool']

for word in lst:
    print(type(word.decode("utf-8")))

或者使用unicode函数:

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

或str.format:

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

答案 1 :(得分:2)

unicode(your_string)完全符合您的需求,我相信。

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

答案 2 :(得分:1)

是的,format()可以使用,但有时不会。旧版本的Python甚至没有它。 我建议:

utext = u"%s" % text

unicode.format()会做同样的事情 如果你不想使用unicode()函数。但显然,你做到了。 :d

答案 3 :(得分:0)

u前缀只能用于文字。要将现有字符串转换为unicode object,请使用unicode() constructor