将unicode列表转换为包含python字符串的列表的简单方法?

时间:2021-02-12 01:34:34

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().

转换后,我实际上正在检查EmployeeList.values()中是否存在“1001”。

6 个解决方案

#1


47  

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.

你需要选择一个有效的编码;不要使用str(),因为它将使用系统默认值(对于Python的ASCII),它不会对Unicode值中的所有可能代码点进行编码。

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.

UTF-8能够编码所有Unicode标准,但ASCII范围之外的任何代码点将导致每个字符多个字节。

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:

但是,如果您只想测试特定的字符串,请测试unicode字符串,并且Python在测试时不必自动编码所有值:

u'1001' in EmployeeList.values()

#2


18  

[str(x) for x in EmployeeList] would do a conversion, but it would fail if the unicode string characters do not lie in the ascii range.

[EmployeeList中的x的str(x)]将进行转换,但如果unicode字符串字符不在ascii范围内,则会失败。

>>> EmployeeList = [u'1001', u'Karick', u'14-12-2020', u'1$']
>>> [str(x) for x in EmployeeList]
['1001', 'Karick', '14-12-2020', '1$']


>>> EmployeeList = [u'1001', u'करिक', u'14-12-2020', u'1$']
>>> [str(x) for x in EmployeeList]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

#3


9  

We can use map function

我们可以使用地图功能

print map(str, EmployeeList)

#4


4  

how about:

def fix_unicode(data):
    if isinstance(data, unicode):
        return data.encode('utf-8')
    elif isinstance(data, dict):
        data = dict((fix_unicode(k), fix_unicode(data[k])) for k in data)
    elif isinstance(data, list):
        for i in xrange(0, len(data)):
            data[i] = fix_unicode(data[i])
    return data

#5


3  

Just simply use this code

只需使用此代码即可

EmployeeList = eval(EmployeeList)
EmployeeList = [str(x) for x in EmployeeList]

#6


0  

Just use

unicode_to_list = list(EmployeeList)

#1


47  

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.

你需要选择一个有效的编码;不要使用str(),因为它将使用系统默认值(对于Python的ASCII),它不会对Unicode值中的所有可能代码点进行编码。

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.

UTF-8能够编码所有Unicode标准,但ASCII范围之外的任何代码点将导致每个字符多个字节。

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:

但是,如果您只想测试特定的字符串,请测试unicode字符串,并且Python在测试时不必自动编码所有值:

u'1001' in EmployeeList.values()

#2


18  

[str(x) for x in EmployeeList] would do a conversion, but it would fail if the unicode string characters do not lie in the ascii range.

[EmployeeList中的x的str(x)]将进行转换,但如果unicode字符串字符不在ascii范围内,则会失败。

>>> EmployeeList = [u'1001', u'Karick', u'14-12-2020', u'1$']
>>> [str(x) for x in EmployeeList]
['1001', 'Karick', '14-12-2020', '1$']


>>> EmployeeList = [u'1001', u'करिक', u'14-12-2020', u'1$']
>>> [str(x) for x in EmployeeList]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

#3


9  

We can use map function

我们可以使用地图功能

print map(str, EmployeeList)

#4


4  

how about:

def fix_unicode(data):
    if isinstance(data, unicode):
        return data.encode('utf-8')
    elif isinstance(data, dict):
        data = dict((fix_unicode(k), fix_unicode(data[k])) for k in data)
    elif isinstance(data, list):
        for i in xrange(0, len(data)):
            data[i] = fix_unicode(data[i])
    return data

#5


3  

Just simply use this code

只需使用此代码即可

EmployeeList = eval(EmployeeList)
EmployeeList = [str(x) for x in EmployeeList]

#6


0  

Just use

unicode_to_list = list(EmployeeList)