问题描述
Json格式:
[{"studios": [{"studioId": 539}, {"studioId": 540}],
"id": 843,
"title": "In the Mood for Love",
"crew": [{"personId": 12453, "department": "Directing", "job": "Director"}, {"personId": 12454, "department": "Sound", "job": "Music"}, {"personId": 12455, "department": "Sound", "job": "Original Music Composer"}, {"personId": 1357, "department": "Camera", "job": "Director of Photography"}, {"personId": 12453, "department": "Writing", "job": "Screenplay"}, {"personId": 12453, "department": "Production", "job": "Producer"}, {"personId": 21909, "department": "Production", "job": "Executive Producer"}, {"personId": 45818, "department": "Editing", "job": "Editor"}, {"personId": 232804, "department": "Camera", "job": "Director of Photography"}, {"personId": 12667, "department": "Camera", "job": "Director of Photography"}],
"releases": [{"releasedate": "2000-11-08", "country": "FR"}, {"releasedate": "2000-05-22", "country": "US"}]
"genres": ["Drama", "Romance"],
"releasedate": "2000-05-22",
"cast": [{"personId": 1337, "character": "Chow Mo-Wan", "order": 0}, {"personId": 1338, "character": "Su Li-Zhen", "order": 1}, {"personId": 12674, "character": "Ah Ping", "order": 2}, {"personId": 12462, "character": "Mrs. Suen", "order": 4}, {"personId": 12463, "character": "Mr. Ho", "order": 5}, {"personId": 12464, "character": "", "order": 6}, {"personId": 12465, "character": "", "order": 7}, {"personId": 12466, "character": "Mr. Chan", "order": 8}, {"personId": 12467, "character": "The Amah", "order": 9}, {"personId": 12468, "character": "", "order": 10}, {"personId": 12469, "character": "", "order": 11}, {"personId": 12470, "character": "Mrs. Chow", "order": 12}],
"userrating": 7.6}]
我正在尝试将其转换为.csv文件。 但是我遇到了错误。 我希望将csv文件在1NF中标准化,以便可以直接传输到某些数据库
我的代码:
import json
import csv
with open("data3.json") as json_file, open("data3.csv", "w",encoding='utf-8') as csv_file:
csv_file = csv.writer(csv_file)
a = json.load(json_file)
csv_file.writerow(["StudiosId", "Id", "Title","personId","Department","Job","ReleaseDate","PosterLink","Genres","Cast","Runtime"])
for item in a:
csv_file.writerow([item["studios"], item["id"], item["title"],item["crew"][0],item["crew"][1],item["crew"][2],item["poster"],item["genres"],item["releasedate"],item["cast"],item["runtime"]])
错误:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
IndexError: list index out of range
1楼
您的JSON结构不正确(缺少逗号),代码看起来还不错。 尝试以下方法:
[{"studios": [{"studioId": 539}, {"studioId": 540}],
"id": 843,
"title": "In the Mood for Love",
"crew": [{"personId": 12453, "department": "Directing", "job": "Director"}, {"personId": 12454, "department": "Sound", "job": "Music"}, {"personId": 12455, "department": "Sound", "job": "Original Music Composer"}, {"personId": 1357, "department": "Camera", "job": "Director of Photography"}, {"personId": 12453, "department": "Writing", "job": "Screenplay"}, {"personId": 12453, "department": "Production", "job": "Producer"}, {"personId": 21909, "department": "Production", "job": "Executive Producer"}, {"personId": 45818, "department": "Editing", "job": "Editor"}, {"personId": 232804, "department": "Camera", "job": "Director of Photography"}, {"personId": 12667, "department": "Camera", "job": "Director of Photography"}],
"releases": [{"releasedate": "2000-11-08", "country": "FR"}, {"releasedate": "2000-05-22", "country": "US"}],
"genres": ["Drama", "Romance"],
"releasedate": "2000-05-22",
"cast": [{"personId": 1337, "character": "Chow Mo-Wan", "order": 0}, {"personId": 1338, "character": "Su Li-Zhen", "order": 1}, {"personId": 12674, "character": "Ah Ping", "order": 2}, {"personId": 12462, "character": "Mrs. Suen", "order": 4}, {"personId": 12463, "character": "Mr. Ho", "order": 5}, {"personId": 12464, "character": "", "order": 6}, {"personId": 12465, "character": "", "order": 7}, {"personId": 12466, "character": "Mr. Chan", "order": 8}, {"personId": 12467, "character": "The Amah", "order": 9}, {"personId": 12468, "character": "", "order": 10}, {"personId": 12469, "character": "", "order": 11}, {"personId": 12470, "character": "Mrs. Chow", "order": 12}],
"userrating": 7.6}]
您提供的示例中也没有poster
或runtime
键。
编辑更新。
import csv
import json
with open('data.json', 'r') as f, open("data3.csv", "w") as csv_file:
csv_file = csv.writer(csv_file)
a = json.load(f)
csv_file.writerow(["StudiosId", "Id", "Title","personId","Department","Job","ReleaseDate","PosterLink","Genres","Cast","Runtime"])
for item in a:
csv_file.writerow([item["studios"], item["id"], item["title"],item["crew"][0]['personId'],item["crew"][0]['department'],item["crew"][0]['job'],item["genres"],item["releasedate"],item["cast"]])