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

List Template:

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

:

 EmployeeList = ['1001', 'Karick', '14-12-2020', '1$'] 

After the conversion, I really check if "1001" exists in EmployeeList.values ​​().

+30
source share
9 answers

Encode each value in the list to a string:

 [x.encode('UTF8') for x in EmployeeList] 

You need to select a valid encoding; do not use str() , as this will use the system standard (for Python 2, which is ASCII), which will not encode all possible code points in a Unicode value.

UTF-8 is capable of encoding the entire Unicode standard, but any code number outside the ASCII range will result in several bytes per character.

However, if all you want to do is test for a specific string, check the unicode string and Python will not automatically encode all values ​​when testing for this:

 u'1001' in EmployeeList.values() 
+51
source

[str(x) for x in EmployeeList] will perform the conversion, but it will not work if the characters in the unicode string are not 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) 
+19
source

We can use the map function

 print map(str, EmployeeList) 
+13
source

Just use this code.

 EmployeeList = eval(EmployeeList) EmployeeList = [str(x) for x in EmployeeList] 
+6
source

What 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
source

Just use

 unicode_to_list = list(EmployeeList) 
0
source

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 this helps you.

0
source

You can do this with 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$'] 
0
source

Just json.dumps will solve the problem

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

Code example:

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

conclusion: ["1001", "Karik", "14-12-2020", "$ 1"]

0
source

All Articles