Python JSON到CSV无法正常工作

时间:2018-04-03 21:06:34

标签: python json csv

我无法将json转换为CSV。 我是JSON解析的新手。我想要的只是来自 我的JSON有些“漂亮”的csv。所以我可以获得可以阅读的数据。

我正在尝试

f = csv.writer(open('data.csv', 'w+', encoding='utf-8', newline=''))
for item in json_head:
    f.writerow(item)

但我进入data.csv的所有内容都是

d,a,t,a
i,n,c,l,u,d,e,d
l,i,n,k,s
m,e,t,a

文档中的Json模板看起来像这样

{
  "data": {
    "type": "string",
    "id": "string",
    "attributes": {
      "createdAt": "string",
      "duration": 0,
      "gameMode": "string",
      "patchVersion": "string",
      "shardId": "string",
      "stats": {},
      "tags": {},
      "titleId": "string"
    },
    "relationships": {
      "assets": {
        "data": [
          {
            "type": "string",
            "id": "string"
          }
        ]
      },
      "rosters": {
        "data": [
          {
            "type": "string",
            "id": "string"
          }
        ]
      },
      "rounds": {},
      "spectators": {}
    },
    "links": {
      "schema": "string",
      "self": "string"
    }
  },
  "included": [
    null
  ],
  "links": {
    "first": "string",
    "previous": "string",
    "self": "string",
    "next": "string"
  },
  "meta": {}
}

我通过API收到的真正JSON的一部分就像这样

{"data":
    {"type":"match","id":"bdd8b91c-8866-47f1-b5ee-45de4163d3ad","attributes":{"createdAt":"2018-04-02T12:16:33Z","duration":1806,"gameMode":"squad-fpp","patchVersion":"","shardId":"pc-eu","stats":null,"tags":null,"titleId":"bluehole-pubg"},"relationships":{"assets":{"data":[{"type":"asset","id":"2609ba26-3674-11e8-b808-0a586466e20b"}]},
    "rosters":{"data":[
    {"type":"roster","id":"80a9215c-bed1-4c11-8ff7-e4c9d643befa"},
    {"type":"roster","id":"1457bd01-80f9-45e3-b98f-aa9ea946f6b2"}]},
    "rounds":{"data":[]},"spectators":{"data":[]}},"links":{"schema":"","self":"https://api.playbattlegrounds.com/shards/pc-eu/matches/bdd8b91c-8866-47f1-b5ee-45de4163d3ad"}},
    "included":[
    {"type":"participant","id":"f0d889c7-49e7-4c83-ac0b-d7f23d13e09f","attributes":{"actor":"","shardId":"pc-eu","stats":{"DBNOs":3,"assists":0,"boosts":2,"damageDealt":253.554047,"deathType":"byplayer","headshotKills":1,"heals":0,"killPlace":4,"killPoints":1182,"killPointsDelta":48.6543655,"killStreaks":0,"kills":4,"lastKillPoints":0,"lastWinPoints":0,"longestKill":5,"mostDamage":0,"name":"TheFieryArrow","playerId":"account.e951c5cf324d4153850fb411a13f4e8d","revives":0,"rideDistance":4288.21143,"roadKills":0,"teamKills":0,"timeSurvived":1215,"vehicleDestroys":0,"walkDistance":1508.55408,"weaponsAcquired":0,"winPlace":15,"winPoints":1113,"winPointsDelta":7.15782833}}},
    {"type":"participant","id":"5c26da97-6733-49d4-8a56-5dc7513e3c77","attributes":{"actor":"","shardId":"pc-eu","stats":{"DBNOs":0,"assists":0,"boosts":0,"damageDealt":64.68,"deathType":"byplayer","headshotKills":0,"heals":0,"killPlace":93,"killPoints":1111,"killPointsDelta":-14.2245054,"killStreaks":0,"kills":0,"lastKillPoints":0,"lastWinPoints":0,"longestKill":0,"mostDamage":0,"name":"Ritter-Sport","playerId":"account.f704b21b861b4c649ec56a0d688171c8","revives":0,"rideDistance":0,"roadKills":0,"teamKills":0,"timeSurvived":142,"vehicleDestroys":0,"walkDistance":43.5367622,"weaponsAcquired":0,"winPlace":28,"winPoints":1220,"winPointsDelta":-19.1033173}}},

首先,如果有办法从特定元素中获取数据就足够了,对我来说,这些元素是"type":"participant" 我希望像这样的表格数据

ID                                    |DBNOs | assists | boosts | othercolumns from "stats"
f0d889c7-49e7-4c83-ac0b-d7f23d13e09f  | 3    |   0     |   2    |
5c26da97-6733-49d4-8a56-5dc7513e3c77  | 0    |   0     |   0    |

我知道我可以硬编码一些代码来解析它,我只是在寻找各种可能性。

由于

1 个答案:

答案 0 :(得分:2)

当你这样做时

for item in json_head:
    f.writerow(item)

它只读取json中的键,然后将其作为字符串传递给writerow方法,该方法将其视为可迭代的字符并将单个字符写入csv。

相反,尝试这样做:

for item in json_head:
    f.writerow([item, json_head[item])