问题是,当我运行脚本时,出现以下错误:
回溯(最近一次通话最后一次):文件“ test1.py”,第16行,在 output.writerow(data [0] .keys())KeyError:0
以下是python代码:
import csv, json, sys
if sys.argv[1] is not None and sys.argv[2] is not None:
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open(fileInput)
outputFile = open(fileOutput, 'w')
data = json.load(inputFile)
inputFile.close()
output = csv.writer(outputFile)
output.writerow(data[0].keys())
for row in data:
output.writerow(row.values())
格式化的JSON数据:
{
"response": [
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T11:20:37",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 1,
"raw_message": "A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.",
"forum": "expressandstar",
"createdAt": "2018-11-15T11:20:37",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-233509073ed3432027d48b1a83f5fbd2/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-233509073ed3432027d48b1a83f5fbd2",
"url": "",
"id": "250728493",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-233509073ed3432027d48b1a83f5fbd2.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T10:40:52",
"disable3rdPartyTrackers": true,
"name": "baggiebuoy"
},
"parent": null,
"isFlagged": false,
"likes": 1,
"id": "4196155749",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>A sinking pound, resignations by the bucketful railroading through a so-called agreement nobody wants. For the good of the country Mrs. May 'go and go now'.</p>",
"isEdited": true,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:37:59",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "This could be heaven, or this could be hell.",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:37:59",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"url": "",
"id": "250739494",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T13:01:02",
"disable3rdPartyTrackers": false,
"name": "Mordecai"
},
"parent": 4196013534,
"isFlagged": false,
"likes": 0,
"id": "4196048572",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>This could be heaven, or this could be hell.</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:36:50",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "The crappest of crap deals.\nWill never get through Parliament.\n\nYou are the weakest link May, goodbye.",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:36:50",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-29e1c59be16c852670e3be302e8c303b/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-29e1c59be16c852670e3be302e8c303b",
"url": "",
"id": "250739494",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-29e1c59be16c852670e3be302e8c303b.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T13:01:02",
"disable3rdPartyTrackers": false,
"name": "Mordecai"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196044068",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>The crappest of crap deals.<br>Will never get through Parliament.</p><p>You are the weakest link May, goodbye.</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:28:13",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "We are all just prisoners here of our own device. \ud83d\ude01",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:28:13",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"url": "",
"id": "274657487",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-12-20T16:45:10",
"disable3rdPartyTrackers": true,
"name": "chaffwolf"
},
"parent": 4195981074,
"isFlagged": false,
"likes": 0,
"id": "4196013534",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>We are all just prisoners here of our own device. \ud83d\ude01</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:27:03",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Should be no deal end off.\nNobody could possibly be the winner so it's simple leave and that's it .\nTheir will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:27:03",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-90ecce8d5dad4396f681182cb470872c/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-90ecce8d5dad4396f681182cb470872c",
"url": "",
"id": "251694793",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-90ecce8d5dad4396f681182cb470872c.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-12T20:20:43",
"disable3rdPartyTrackers": true,
"name": "wanderer in eire"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196012501",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Should be no deal end off.<br>Nobody could possibly be the winner so it's simple leave and that's it .<br>Their will always be wannabes who think they can do better in it for only their own powers not for the general country or the people of it .,</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T10:26:42",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Damned if she does, damned if she doesn't. All for a cause she does not believe in.\nShe will go down in history whatever happens. \nThe question is: who better to lead the country at such an important juncture??\nAnswers on a postcard!",
"forum": "expressandstar",
"createdAt": "2018-11-15T10:26:42",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-42ba513c42a0fd6558aa44b1de658140/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-42ba513c42a0fd6558aa44b1de658140",
"url": "",
"id": "274657487",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-42ba513c42a0fd6558aa44b1de658140.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-12-20T16:45:10",
"disable3rdPartyTrackers": true,
"name": "chaffwolf"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4196012237",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Damned if she does, damned if she doesn't. All for a cause she does not believe in.<br>She will go down in history whatever happens. <br>The question is: who better to lead the country at such an important juncture??<br>Answers on a postcard!</p>",
"isEdited": false,
"isApproved": true
},
{
"thread": "7046384220",
"numReports": 0,
"editableUntil": "2018-11-22T09:50:32",
"sb": false,
"media": [],
"moderationLabels": [],
"isSpam": false,
"isHighlighted": false,
"dislikes": 0,
"points": 0,
"raw_message": "Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.\n\nYou can check out any time you like, but you can never leave.",
"forum": "expressandstar",
"createdAt": "2018-11-15T09:50:32",
"canVote": false,
"author": {
"profileUrl": "https://disqus.com/by/mnaid-e82a88d937e60267fd2c866b01131ada/",
"isPrivate": false,
"isPrimary": true,
"about": "",
"username": "mnaid-e82a88d937e60267fd2c866b01131ada",
"url": "",
"id": "250727584",
"isAnonymous": false,
"avatar": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png",
"small": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar32.png"
},
"large": {
"permalink": "https://disqus.com/api/users/avatars/mnaid-e82a88d937e60267fd2c866b01131ada.jpg",
"cache": "//a.disquscdn.com/1541535443/images/noavatar92.png"
},
"isCustom": false
},
"location": "",
"isPowerContributor": false,
"signedUrl": "",
"joinedAt": "2017-05-03T10:27:23",
"disable3rdPartyTrackers": false,
"name": "Olly the cat"
},
"parent": null,
"isFlagged": false,
"likes": 0,
"id": "4195981074",
"isDeletedByAuthor": false,
"isDeleted": false,
"message": "<p>Theresa May is using The Eagles Hotel California lyrics as the inspiration for her Brexit plan.</p><p>You can check out any time you like, but you can never leave.</p>",
"isEdited": false,
"isApproved": true
}
],
"code": 0,
"cursor": {
"more": false,
"total": null,
"prev": null,
"hasNext": false,
"next": "1542275432008325:1:0",
"id": "1542275432008325:1:0",
"hasPrev": false
}
}
我的主要项目是从JSON文件中获取数据,并使用该数据建立关系数据库。
答案 0 :(得分:0)
该JSON数据中的顶级对象是字典。但是您正在尝试将其索引为列表。 (data[0]
)。由于0不是字典的键,因此尝试将其用作键会导致KeyError
。
如果要使用顶部键对象的键,请使用data.keys()
。