我希望能够添加一个'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'
你可以解码:
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)))
我相信unicode(your_string)
正是您所需要的。
>>> unicode("Hello world"!)
u"Hello world!"
>>> print (unicode("Hello world"!))
"Hello world!"
是的,format()可以工作,但有时不能。旧版本的Python甚至没有这个功能。我建议:
utext = u"%s" % text
的作用与unicode.format()相同。如果您不喜欢使用unicode()函数。但很明显,你知道。: D
u
前缀只能用于文字。要将现有字符串转换为unicode对象,请使用unicode()
构造函数。