具有一组json,需要将其转换为csv

时间:2019-03-05 09:50:20

标签: python json csv

{"i": "56", "m1": "IT", "m2": "Area", "r": "faced this problem"}

{"i": "57", "m1": "IT", "m2": "Area", "r": "faced this problem3"}

这是JSON的格式吗?

1 个答案:

答案 0 :(得分:0)

代码

import json

with open('input.json', "r") as in_file:
    data = json.load(in_file)

with open('output.csv', 'w') as out_file:
    for entry in data:
        out_file.write(','.join(entry.values()) + '\n')

input.json

[
  {
    "i": "56",
    "m1": "IT",
    "m2": "Area",
    "r": "faced this problem"
  },
  {
    "i": "57",
    "m1": "IT",
    "m2": "Area",
    "r": "faced this problem3"
  }
]

output.csv

56,IT,Area,faced this problem
57,IT,Area,faced this problem3