正确将JSON格式转换为CSV吗?

时间:2019-12-16 19:27:57

标签: python json csv

我有一个要转换为CSV文件的JSON URL。我该如何使用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"}]

3 个答案:

答案 0 :(得分:1)

尝试使用熊猫,如下所示:

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')

答案 1 :(得分:0)

根据您格式错误的CSV,我猜r.text包含一个字典列表。 因此,您必须首先写入标题行,然后循环遍历所有行,然后逐行写入数据。尝试这样的事情:

with open('C:/test/filename.csv', 'w+') as f:

    fieldnames = ["latitude", "longitude"]
    writer = csv.DictWriter(f, fieldnames=fieldnames)    
    writer.writeheader()

    for row in r.text:
        writer.writerow(row)

答案 2 :(得分:0)

您需要使用csv模块写出文件。特别是,您需要一个DictWriter实例。

此外,您似乎正在从服务器获取JSON。因此,您需要json模块来解析它。

下面是经过测试的代码示例。

import json, requests

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

output = json.loads(r.text)
fieldnames = output[0].keys()

with open('C:/test/filename.csv', 'w+') as f:
    dw = csv.DictWriter(f,fieldnames=fieldnames)
    dw.writeheader()
    dw.writerows(output)
相关问题