python - 将非嵌套 json 转换为 csv 文件?

标签 python json csv

我正在使用一个非嵌套的json文件,数据来自reddit。我正在尝试使用 python 将其转换为 csv 文件。每行没有相同的字段,因此不断收到错误:

JSONDecodeError: Extra data: line 2 column 1

这是代码:

import csv
import json
import os

os.chdir('c:\\Users\\Desktop')
infile = open("data.json", "r")
outfile = open("outputfile.csv", "w")

writer = csv.writer(outfile)

for row in json.loads(infile.read()):
    writer.writerow(row)

以下是数据中的几行:

{"author":"i_had_an_apostrophe","body":"\"It's not your fault.\"","author_flair_css_class":null,"link_id":"t3_5c0rn0","subreddit":"AskReddit","created_utc":1478736000,"subreddit_id":"t5_2qh1i","parent_id":"t1_d9t3q4d","author_flair_text":null,"id":"d9tlp0j"}
{"id":"d9tlp0k","author_flair_text":null,"parent_id":"t1_d9tame6","link_id":"t3_5c1efx","subreddit":"technology","created_utc":1478736000,"subreddit_id":"t5_2qh16","author":"willliam971","body":"9/11 inside job??","author_flair_css_class":null}
{"created_utc":1478736000,"subreddit_id":"t5_2qur2","link_id":"t3_5c44bz","subreddit":"excel","author":"excelevator","author_flair_css_class":"points","body":"Have you tried stepping through the code to analyse the values at each step?\n\n","author_flair_text":"442","id":"d9tlp0l","parent_id":"t3_5c44bz"}
{"created_utc":1478736000,"subreddit_id":"t5_2tycb","link_id":"t3_5c384j","subreddit":"OldSchoolCool","author":"10minutes_late","author_flair_css_class":null,"body":"**Thanks Hillary**","author_flair_text":null,"id":"d9tlp0m","parent_id":"t3_5c384j"}

我正在考虑获取 csv 文件中可用的所有字段(作为标题),如果该特定字段没有可用数据,只需用 NA 填充即可。

最佳答案

您的问题缺少有关您想要完成的任务的信息,因此我对它们进行猜测。请注意,csv 文件不使用“null”来表示缺失的字段,它们只是具有分隔符,中间没有任何内容,例如 1,2,,4,5它没有第三个字段值。

此外,打开 csv 文件的方式也会有所不同,具体取决于您使用的是 Python 2 还是 3。下面的代码适用于 Python 3。

#!/usr/bin/env python3
import csv
import json
import os

os.chdir('c:\\Users\\Desktop')
with open('sampledata.json', 'r', newline='') as infile:
    data = json.loads(infile.read())

# determine all the keys present, which will each become csv fields
fields = list(set(key for row in data for key in row))

with open('outputfile.csv', 'w', newline='') as outfile:
    writer = csv.DictWriter(outfile, fields)
    writer.writeheader()
    writer.writerows(row for row in data)

关于python - 将非嵌套 json 转换为 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885157/

相关文章:

javascript - 我无法获取 Json 对象属性

java - 计算 JSONArray 中的字符串和 JSONObject 的数量

php - 在 PHP 中为用户创建一个 CSV 文件

python - 在 csv 文件中标记重复项

bash - grep 获取 csv 文件的值

python - 从 python 打开 Windows 照片库

python - input(), raw_input(), 将输入回显为星号

python - 检查 Pandas 数据框的异常值

python - 更改音频数据时出现噪音

javascript - 如何根据边的数组对象内的数据选择边?