python - 正确地将 JSON 转换并格式化为 CSV?

标签 python json csv

我有一个 JSON URL,想要转换为 CSV 文件。我怎样才能用Python做到这一点?

我尝试过:

import requests
import csv

params = {
  "format": "csv"
  }
r = requests.get('https://myaccount.srpnet.com/myaccountapi/api/outages/getall', params=params)
print(r.text)

# This bit of code will write the result of the query to output.csv

with open('C:/test/filename.csv', 'w+') as f:
    fieldnames = ["latitude"], ["longitude"]
    f.write(r.text)

输出 .csv 如下所示:

"[{""latitude"":33.6031326225"  longitude:-112.193951324    numberCustomersAffected:1   "outageProblem:""An underground power cable has failed. SRP crews are working to restore power."""  isMaintenanceOutage:false   estimatedUsersImpacted:0    "crossRoadText:""N 67TH AVE to N 59TH AVE and W THUNDERBIRD RD to W CACTUS RD"""    "estimatedRestorationTime:""2019-12-16T21:35:00Z""" "outageBegan:""2019-12-16T15:33:27Z"""  "outageId:""486858""}"  "{""latitude"":33.279829655"    longitude:-111.772819298    numberCustomersAffected:1   "outageProblem:""An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible."""  isMaintenanceOutage:true    estimatedUsersImpacted:0    "crossRoadText:""S GILBERT RD to S QUARTZ ST and E PECOS RD to E GERMANN RD"""  "estimatedRestorationTime:""2019-12-16T23:00:00Z""" "outageBegan:""2019-12-16T14:59:11Z"""  "outageId:""486875""}"  "{""latitude"":33.4974939093"   longitude:-111.984698683    numberCustomersAffected:16  "outageProblem:""An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible."""  isMaintenanceOutage:true    estimatedUsersImpacted:0    "crossRoadText:""N 44TH ST to N 48TH ST and E CAMPBELL AVE to E INDIAN SCHOOL RD""" "estimatedRestorationTime:""2019-12-16T21:50:00Z""" "outageBegan:""2019-12-16T15:48:35Z"""  "outageId:""486882""}"  "{""latitude"":33.377234657"    longitude:-111.644471686    numberCustomersAffected:11  "outageProblem:""We continue to investigate the cause."""   isMaintenanceOutage:false   estimatedUsersImpacted:0    "crossRoadText:""S HAWES RD to S ELLSWORTH RD and E BASELINE RD to E STEVEN POLLARD AVE"""  "estimatedRestorationTime:""2019-12-16T20:40:00Z""" "outageBegan:""2019-12-16T18:35:56Z"""  "outageId:""486922""}"  "{""latitude"":33.1435118621"   longitude:-111.521769468    numberCustomersAffected:12  "outageProblem:""An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible."""  isMaintenanceOutage:true    estimatedUsersImpacted:0    "crossRoadText:""N TOURMALINE DR to N SIERRA VISTA RD and E JUDD RD to W SILVERDALE RD"""   "estimatedRestorationTime:""2019-12-16T20:50:00Z""" "outageBegan:""2019-12-16T18:49:03Z"""  "outageId:""486924""}]"

我希望它看起来像这样:

latitude    longitude   numberCustomersAffected outageProblem   isMaintenanceOutage estimatedUsersImpacted  crossRoadText   estimatedRestorationTime    outageBegan outageId
33.60313262 -112.1939513    1   An underground power cable has failed. SRP crews are working to restore power.  FALSE   0   N 67TH AVE to N 59TH AVE and W THUNDERBIRD RD to W CACTUS RD    2019-12-16T21:35:00Z    2019-12-16T15:33:27Z    486858
33.50462573 -111.9944968    7   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   N 36TH ST to N 44TH ST and E CAMELBACK RD to E CAMPBELL AVE 2019-12-16T19:45:00Z    2019-12-16T14:41:08Z    486872
33.41878816 -111.6283175    2   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   N ELLSWORTH RD to N 96TH ST and E UNIVERSITY DR to E APACHE TRL 2019-12-16T16:45:00Z    2019-12-16T14:44:38Z    486873
33.51363707 -112.0268558    6   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   N 24TH ST to N 28TH ST and E MISSOURI AVE to E CAMELBACK RD 2019-12-16T16:55:00Z    2019-12-16T14:53:49Z    486874
33.27982966 -111.7728193    1   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   S GILBERT RD to S QUARTZ ST and E PECOS RD to E GERMANN RD  2019-12-16T23:00:00Z    2019-12-16T14:59:11Z    486875
33.44074458 -112.1861484    0   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   S 63RD AVE to S 55TH AVE and W VAN BUREN ST to W BUCKEYE RD 2019-12-16T18:05:00Z    2019-12-16T15:02:40Z    486876
33.41773662 -111.9926656    0   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   S 40TH ST to S 43RD WAY and E UNIVERSITY DR to E ELWOOD ST  2019-12-16T19:30:00Z    2019-12-16T15:26:58Z    486879
33.33877131 -111.7891725    2   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   S 132ND ST to S LINDSAY RD and W HACKAMORE AVE to E WARNER RD   2019-12-16T16:40:00Z    2019-12-16T15:35:35Z    486880
33.49749391 -111.9846987    16  An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   N 44TH ST to N 48TH ST and E CAMPBELL AVE to E INDIAN SCHOOL RD 2019-12-16T21:50:00Z    2019-12-16T15:48:35Z    486882
33.40325351 -111.8665199    12  An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   S DOBSON RD to S ALMA SCHOOL RD and W BROADWAY RD to W 8TH AVE  2019-12-16T20:45:00Z    2019-12-16T15:41:30Z    486883
33.5133441  -112.1491916    11  An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   GRAND AVE to N 39TH AVE and W MISSOURI AVE to W CAMELBACK RD    2019-12-16T18:45:00Z    2019-12-16T15:44:40Z    486884
33.50472637 -111.9348527    14  An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   N INVERGORDON RD to N SCOTTSDALE RD and E CHAPARRAL RD to E CAMELBACK RD    2019-12-16T17:50:00Z    2019-12-16T15:49:44Z    486886
33.51471289 -112.2795876    7   An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.  TRUE    0   N 107TH AVE to N 99TH AVE and W BETHANY HOME RD to W CAMELBACK RD   2019-12-16T18:55:00Z    2019-12-16T15:52:04Z    486887

JSON API 请求如下所示:

[{"latitude":33.6031326225,"longitude":-112.193951324,"numberCustomersAffected":1,"outageProblem":"An underground power cable has failed. SRP crews are working to restore power.","isMaintenanceOutage":false,"estimatedUsersImpacted":0,"crossRoadText":"N 67TH AVE to N 59TH AVE and W THUNDERBIRD RD to W CACTUS RD","estimatedRestorationTime":"2019-12-16T21:35:00Z","outageBegan":"2019-12-16T15:33:27Z","outageId":"486858"},{"latitude":33.279829655,"longitude":-111.772819298,"numberCustomersAffected":1,"outageProblem":"An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.","isMaintenanceOutage":true,"estimatedUsersImpacted":0,"crossRoadText":"S GILBERT RD to S QUARTZ ST and E PECOS RD to E GERMANN RD","estimatedRestorationTime":"2019-12-16T23:00:00Z","outageBegan":"2019-12-16T14:59:11Z","outageId":"486875"},{"latitude":33.4974939093,"longitude":-111.984698683,"numberCustomersAffected":16,"outageProblem":"An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.","isMaintenanceOutage":true,"estimatedUsersImpacted":0,"crossRoadText":"N 44TH ST to N 48TH ST and E CAMPBELL AVE to E INDIAN SCHOOL RD","estimatedRestorationTime":"2019-12-16T21:50:00Z","outageBegan":"2019-12-16T15:48:35Z","outageId":"486882"},{"latitude":33.377234657,"longitude":-111.644471686,"numberCustomersAffected":11,"outageProblem":"We are investigating the cause of the outage.","isMaintenanceOutage":false,"estimatedUsersImpacted":0,"crossRoadText":"S HAWES RD to S ELLSWORTH RD and E BASELINE RD to E STEVEN POLLARD AVE","estimatedRestorationTime":"2019-12-16T20:40:00Z","outageBegan":"2019-12-16T18:35:56Z","outageId":"486922"},{"latitude":33.1435118621,"longitude":-111.521769468,"numberCustomersAffected":12,"outageProblem":"An SRP maintenance crew is performing critical maintenance work to repair or upgrade equipment. Power will be restored as quickly as possible.","isMaintenanceOutage":true,"estimatedUsersImpacted":0,"crossRoadText":"N TOURMALINE DR to N SIERRA VISTA RD and E JUDD RD to W SILVERDALE RD","estimatedRestorationTime":"2019-12-16T20:50:00Z","outageBegan":"2019-12-16T18:49:03Z","outageId":"486924"}]

最佳答案

尝试使用 pandas 如下:

import requests
import json
import pandas

r = requests.get('https://myaccount.srpnet.com/myaccountapi/api/outages/getall')
data = r.json()
pandas.read_json(json.dumps(data)).to_csv('pandas.csv')

关于python - 正确地将 JSON 转换并格式化为 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59363106/

相关文章:

python - 稀疏矩阵上的逐元素运算

python - paramiko 没有现有的 session 异常

python - json.dumps() : escaping forward slashes

java - 使用 Java 解析 Facebook signed_request 返回格式错误的 JSON

java - 如何将jgrapht简单图序列化为json?

java - 如何确保我的 .jar 从独立文件中读取数据

python - 提取十六进制编码字符串的一部分

python - 如何使用递归但不使用 for 或 while 循环来获得不重复的字符串排列?

read.csv 将空字符列转换为 NA

perl - 文本处理 - 输出到 csv 文件