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

前端 未结 9 2093
臣服心动
臣服心动 2020-12-23 16:42

Template of the list is:

EmployeeList =  [u\'\', u\'\', u\'\', u\'\']

I would like to con

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 17:19

    [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 "", line 1, in 
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
    

提交回复
热议问题