我有一个csv,其中一列值在dict列表中,如下所示
[{'10': 'i ve been with what is now comcast since 2001 the company has really grown and improved and delivers a great service along with great customer service ', 'aspects':['service']},
{'20': 'good service but lack of options to allow it be more affordable allowing individual channel choices would be great ', 'aspects':['lack', 'service']},
{'30': 'it a good service but very expensive', 'aspects':['service']}, {'40': 'good service', 'aspects':['service']},
{'50': 'good service but over priced ', 'aspects':['service']}]
现在因为当我从CSV
开始阅读string
时,我无法将其转换为原始类型list of dict
,然后json
。
我如何才能真正实现这一目标。
解决方案:
data = output[output.aspects == aspect]['column1'].tolist()
listData=ast.literal_eval(data[0])
return json.dumps(listData)
答案 0 :(得分:3)
您可以使用ast
模块
<强>实施例强>
import ast
s = """[{'10': 'i ve been with what is now comcast since 2001 the company has really grown and improved and delivers a great service along with great customer service ', 'aspects':['service']},
{'20': 'good service but lack of options to allow it be more affordable allowing individual channel choices would be great ', 'aspects':['lack', 'service']},
{'30': 'it a good service but very expensive', 'aspects':['service']}, {'40': 'good service', 'aspects':['service']},
{'50': 'good service but over priced ', 'aspects':['service']}]"""
print(ast.literal_eval(s))
<强>输出:强>
[{'10': 'i ve been with what is now comcast since 2001 the company has really grown and improved and delivers a great service along with great customer service ', 'aspects': ['service']}, {'aspects': ['lack', 'service'], '20': 'good service but lack of options to allow it be more affordable allowing individual channel choices would be great '}, {'30': 'it a good service but very expensive', 'aspects': ['service']}, {'aspects': ['service'], '40': 'good service'}, {'aspects': ['service'], '50': 'good service but over priced '}]
答案 1 :(得分:-1)
转换为json文件:
>>> import json
>>> json.dump(obj,open(path + '/txt.json','w+'))