用u'...'将字符串列表转换为普通字符串列表 [英] Converting list of strings with u'...' to a list of normal strings
问题描述
我是python的新手.并对一个非常基本的问题表示歉意.
I'm a newbie in python. And apologies for a very basic question.
我正在使用python pattern.en
库,并尝试获取单词的同义词.这是我的代码,工作正常.
I'm working with python pattern.en
library and try to get the synonyms of a word. this is my code and is working fine.
from pattern.en import wordnet
a=wordnet.synsets('human')
print a[0].synonyms
这是我从中得到的输出:
this what the output i get from this:
[u'homo', u'man', u'human being', u'human']
但是对于我的程序,我需要这样插入此数组:
but for my program i need to insert this array as this:
['homo', 'man', 'human being', 'human']
如何获得上述输出并从输出中删除"u".
how do i get an output as above and remove the 'u' from my output.
先谢谢..!
推荐答案
尝试正确使用编码-但请注意,此 u
对数据没有任何影响-它只是unicode对象(而不是字节数组)的显式表示,如果您的代码需要返回unicode
则更好喂它unicode.
Try proper encoding- But care this u
does not have any effect on data- it is just an explicit representation of unicode object (not byte array), if your code needs back unicode
then better to feed it unicode.
>>>d = [u'homo', u'man', u'human being', u'human']
>>>print [i.encode('utf-8') for i in d]
>>>['homo', 'man', 'human being', 'human']
这篇关于用u'...'将字符串列表转换为普通字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!