如何将这个CSV转换成Json?

2024-04-26 00:16:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将这个CSV转换成定制的JSON格式(我在下面附上了我的代码研究,无法获得所需的输出结构) 我在数据帧中有csv

将此csv转换为json

enter image description here

从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"
            },

        .....

Tags: csvnamejsonyouremailitcol2col3
1条回答
网友
1楼 · 发布于 2024-04-26 00:16:39

TL;博士

给出:

$ 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'}]]

相关问题 更多 >