Easy way to convert a unicode list to a list conta

2019-01-31 03:16发布

问题:

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

回答1:

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


回答2:

[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 = [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:

We can use map function

print map(str, EmployeeList)


回答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:

Just simply use this code

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


回答6:

Just use

unicode_to_list = list(EmployeeList)


回答7:

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.



回答8:

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

>>> 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$']