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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18272066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:20:56  来源:igfitidea点击:

Easy way to convert a unicode list to a list containing python strings?

pythonpython-2.7unicodeencoding

提问by Karthick

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”。

回答by DhruvPathak

[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.

[str(x) for x in EmployeeList]会进行转换,但如果 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)

回答by Martijn Pieters

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 2 是 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 unicodestring and Python won't have to auto-encode all values when testing for that:

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

u'1001' in EmployeeList.values()

回答by le_vine

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

回答by Mani

We can use mapfunction

我们可以使用map函数

print map(str, EmployeeList)

回答by Umar Asghar

Just simply use this code

只需简单地使用此代码

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

回答by Manish Yadav

Just use

只需使用

unicode_to_list = list(EmployeeList)

回答by SHIVAPUTRA UDAGATTI

There are several ways to do this. I converted like this

有几种方法可以做到这一点。我是这样转换的

def clean(s):
    s = s.replace("u'","")
    return re.sub("[\[\]\'\s]", '', s)

EmployeeList = [clean(i) for i in str(EmployeeList).split(',')]

After that you can check

之后你可以检查

if '1001' in EmployeeList:
    #do something

Hope it will help you.

希望它会帮助你。

回答by Gopikrishna

You can do this by using json and ast modules as follows

您可以通过使用 json 和 ast 模块来做到这一点,如下所示

>>> import json, ast
>>>
>>> EmployeeList =  [u'1001', u'Karick', u'14-12-2020', u'1$']
>>>
>>> result_list = ast.literal_eval(json.dumps(EmployeeList))
>>> result_list
['1001', 'Karick', '14-12-2020', '1$']

回答by Praveen Kumar

Just json.dumps will fix the problem

只需 json.dumps 即可解决问题

json.dumps function actually converts all the unicode literals to string literals and it will be easy for us to load the data either in json file or csv file.

json.dumps 函数实际上将所有 unicode 文字转换为字符串文字,我们很容易在 json 文件或 csv 文件中加载数据。

sample code:

示例代码:

import json
EmployeeList =  [u'1001', u'Karick', u'14-12-2020', u'1$']
result_list = json.dumps(EmployeeList)
print result_list

output: ["1001", "Karick", "14-12-2020", "1$"]

输出:[“1001”,“Karick”,“14-12-2020”,“1$”]