Reputation: 533
I have these unicodes:
uni_list = u'["","aa","bb","cc"]
I also have the following unicodes:
uni_str = u'dd'
I need to combine them together to a list, and get rid of null ones, the desired result will be like:
["aa","bb","cc","dd"]
But I don't know when it will be a uni_list, or a uni_str as i am reading a json file which split these results, is there a unified solution to convert them and combine them to a python list or set?
I tried to use ast.literal_eval, it seems only handle uni_list, but give me error of "malformed string" when it is a uni_str.
Many Thanks!
Upvotes: 1
Views: 756
Reputation: 92854
Alternative solution using re.match and re.findall functions:
result = []
def getValues(s):
global result
// check if input string contains list representation
is_list = re.match(r'^\[[^][]+\]$', s, re.UNICODE)
if is_list:
result = result + re.findall(r'\"([^",]+)\"', s, re.UNICODE)
else:
result.append(s)
getValues(u'["","aa","bb","cc"]')
getValues(u'dd')
print(result)
The output:
['aa', 'bb', 'cc', 'dd']
Upvotes: 1
Reputation: 48092
You may use ast.literal_eval
to convert your string to list as:
>>> import ast
>>> my_unicode = u'["","aa","bb","cc"]'
# convert string to list
>>> my_list = ast.literal_eval(my_unicode)
>>> my_list
['', 'aa', 'bb', 'cc']
# Filter empty string from list
>>> new_list = [i for i in my_list if i]
>>> new_list
['aa', 'bb', 'cc']
# append `"dd"` string to the list
>>> new_list.append("dd") # OR, `str(u"dd")` if `"dd"` is unicode string
>>> new_list
['aa', 'bb', 'cc', 'dd']
Upvotes: 4