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

前端 未结 9 2049
臣服心动
臣服心动 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 "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
    
    0 讨论(0)
  • 2020-12-23 17:26

    Just use

    unicode_to_list = list(EmployeeList)
    
    0 讨论(0)
  • 2020-12-23 17:26

    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.

    0 讨论(0)
  • 2020-12-23 17:30

    Just json.dumps will fix the problem

    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.

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

    0 讨论(0)
  • 2020-12-23 17:32

    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$']
    
    0 讨论(0)
  • 2020-12-23 17:34

    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
    
    0 讨论(0)
提交回复
热议问题