问题描述
列表的模板是:
EmployeeList = [u'< EmpId>',u'< Name> ',u'< Doj>',u'< Salary>']
我想从这个
EmployeeList = [u'1001',u'Karick',u'14-12-2020' u'1 $']
到:
EmployeeList = ['1001','Karick','14 -12-2020','1 $']
转换后,我实际上检查EmployeeList.values()中是否存在1001。
将列表中的每个值编码为一个字符串:
[x.encode('UTF8 ')for x in EmployeeList]
您需要选择有效的编码;不要使用 str()
,因为这将使用系统默认值(对于Python 2为ASCII),不会对Unicode值中的所有可能的代码点进行编码。
UTF-8能够对所有Unicode标准进行编码,但ASCII范围之外的任何代码点将导致每个字符多个字节。
但是,如果你想做的是测试一个特定的字符串,测试一个 unicode 字符串,Python将不必在测试时自动对所有值进行编码: / p>
u'1001'在EmployeeList.values()
Template of the list is:
EmployeeList = [u'<EmpId>', u'<Name>', u'<Doj>', u'<Salary>']
I would like to convert from this
EmployeeList = [u'1001', u'Karick', u'14-12-2020', u'1$']
to this:
EmployeeList = ['1001', 'Karick', '14-12-2020', '1$']
After conversion, I am actually checking if "1001" exists in EmployeeList.values().
Encode each value in the list to a string:
[x.encode('UTF8') for x in EmployeeList]
You need to pick a valid encoding; don't use str()
as that'll use the system default (for Python 2 that's ASCII) which will not encode all possible codepoints in a Unicode value.
UTF-8 is capable of encoding all of the Unicode standard, but any codepoint outside the ASCII range will lead to multiple bytes per character.
However, if all you want to do is test for a specific string, test for a unicode string and Python won't have to auto-encode all values when testing for that:
u'1001' in EmployeeList.values()
这篇关于将unicode列表转换为包含python字符串的列表的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!