如何将此CSV转换为自定义JSON格式,(我在下面附加了我的代码研究,无法获得所需的输出结构) 我在pandas数据框中有CSV
将此csv转换为json
从csv到json
[
{
"name": "your_name",
"email": "your_email"
},
[
[
{
"col1": "Phoebe",
"col2": "Oh my God, he\u0092s lost it. He\u0092s totally lost it.",
"col3": "PREDICTED_EMOTION"
},
{
"col1": "Monica",
"col2": "What?",
"col3": "PREDICTED_EMOTION"
},
.....
答案 0 :(得分:0)
给出:
$ cat x.csv
col1 col2 col3
0.123 this is a text txt
0.987 whatever this is spam
0.429 yummy, frites fries
对于列作为json键:
>>> import pandas as pd
>>> df = pd.read_csv('x.csv', sep='\t')
>>> df
col1 col2 col3
0 0.123 this is a text txt
1 0.987 whatever this is spam
2 0.429 yummy, frites fries
>>> df.to_json()
'{"col1":{"0":0.123,"1":0.987,"2":0.429},"col2":{"0":"this is a text","1":"whatever this is","2":"yummy, frites"},"col3":{"0":"txt","1":"spam","2":"fries"}}'
对于作为json键的行:
>>> df.T
0 1 2
col1 0.123 0.987 0.429
col2 this is a text whatever this is yummy, frites
col3 txt spam fries
>>> df.T.to_json()
'{"0":{"col1":0.123,"col2":"this is a text","col3":"txt"},"1":{"col1":0.987,"col2":"whatever this is","col3":"spam"},"2":{"col1":0.429,"col2":"yummy, frites","col3":"fries"}}'
所需的json是结构不良的json。上面的熊猫默认设置是更合理的。
如果您确实希望按照OP进行输出,则:
>>> from pprint import pprint
>>> import json
>>> import pandas as pd
>>> df = pd.read_csv('x.csv', sep='\t')
>>> tmp_json = df.T.to_json()
>>> tmp_json
'{"0":{"col1":0.123,"col2":"this is a text","col3":"txt"},"1":{"col1":0.987,"col2":"whatever this is","col3":"spam"},"2":{"col1":0.429,"col2":"yummy, frites","col3":"fries"}}'
>>> [v for k,v in eval(tmp_json).items()]
[{'col1': 0.123, 'col2': 'this is a text', 'col3': 'txt'}, {'col1': 0.987, 'col2': 'whatever this is', 'col3': 'spam'}, {'col1': 0.429, 'col2': 'yummy, frites', 'col3': 'fries'}]
>>> json.dumps([{"name": "your_name", "email": "your_email"}, tmp_json_dict])
'[{"name": "your_name", "email": "your_email"}, [{"col1": 0.123, "col2": "this is a text", "col3": "txt"}, {"col1": 0.987, "col2": "whatever this is", "col3": "spam"}, {"col1": 0.429, "col2": "yummy, frites", "col3": "fries"}]]'
>>> op_desired_json = json.dumps([{"name": "your_name", "email": "your_email"}, tmp_json_dict])
>>> pprint(eval(op_desired_json))
[{'email': 'your_email', 'name': 'your_name'},
[{'col1': 0.123, 'col2': 'this is a text', 'col3': 'txt'},
{'col1': 0.987, 'col2': 'whatever this is', 'col3': 'spam'},
{'col1': 0.429, 'col2': 'yummy, frites', 'col3': 'fries'}]]