【问题标题】:How to convert this CSV into Json?如何将此 CSV 转换为 Json?
【发布时间】:2019-05-19 06:25:20
【问题描述】:

如何将此 CSV 转换为自定义 JSON 格式,(我在下面附上了我的代码研究,无法获得所需的输出结构) 我在熊猫数据框中有 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"
            },

        .....

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    TL;DR

    给定:

    $ 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。上面pandas默认的那个更合理。

    如果你真的想要按照 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'}]]
    

    【讨论】:

    • 嗨@alvas,我想将csv pandas 数据框转换为上述json 格式。
    • 姓名和电子邮件从何而来?它们是任意的,除非它们在 CSV 中,否则无法读取。
    • 而且您提供的不是结构不良的 json。 pandas 导出的内容(对于作为 json 键的行)更合理,至少它可以将行的顺序查询为集合/字典而不是顺序列表。
    • 是的,那么它们来自哪里?
    • 这是提交输出文件,所以我需要在提交此 json 时在 json 中添加我的电子邮件 ID 和名称。
    猜你喜欢
    • 2022-06-13
    • 2018-09-16
    • 2020-08-20
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多