【问题标题】:Iterate through each row of CSV and write into individual JSON files in a folder using Python-Pandas遍历 CSV 的每一行并使用 Python-Pandas 写入文件夹中的单个 JSON 文件
【发布时间】:2019-12-24 05:18:05
【问题描述】:

我正在尝试使用输入为 CSV 为每一行生成单独的 JSON 文件并保存到输出文件夹中。使用循环逻辑。

【问题讨论】:

    标签: python json python-3.x pandas python-2.7


    【解决方案1】:

    假设data 是您问题中定义的字典,则以下代码可以满足您的需求:

    data_path = 'C:/Users/usasxs174/Desktop/ETL/Data_tables/MT/'
    for i, data_item in enumerate(data['entities']):
        with open(data_path + str(i) + '.json', 'w') as outfile:
            outfile.write(json.dumps(data_item, indent=4))
    

    【讨论】:

    • 谢谢。但是我正在寻找将“数据”字典转换为单独的 json 文件。
    • @satyaveni,我已经编辑了代码。现在,它将data 字典中的数据保存在for 循环中,以分隔JSON 文件。如果不是您要查找的内容,请编辑您的问题以指定每个 JSON 文件的所需输出。
    • 很高兴能帮上忙。
    • 尝试替换此行代码的最后一行:outfile.write(json.dumps({'entities': [data_item]}, indent=4))。这是你想要的输出吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多