我正在尝试编写一个Python脚本,它将检索18组JSON数据并将它们写入我可用于D3项目的单个文件中。我只用自己的Python教了一个星期,所以我确定我用一些简单的方法弄乱了它。我非常感谢帮助找到我的错误。这是迄今为止的剧本。
# Import libraries.
import requests
import json
hucs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
for huc in hucs:
# create request as json, dynamically altering the url to retrieve one HUC at a time.
if(huc < 10):
data = requests.get('http://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&huc=0{}&startDT=2015-05-22&endDT=2015-05-22¶meterCd=00060&siteType=OC,OC-CO,ES,LK,ST,ST-CA,ST-DCH,ST-TS'.format(huc), stream=True).json()
else:
data = requests.get('http://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&huc={}&startDT=2015-05-22&endDT=2015-05-22¶meterCd=00060&siteType=OC,OC-CO,ES,LK,ST,ST-CA,ST-DCH,ST-TS'.format(huc), stream=True).json()
# access timeSeries
time_series = data['value']['timeSeries']
# using open, create a file called data.json which we'll refer to as "output" in the code below
with open('data/data.json', 'wb') as output:
# create a single root element.
root = bytes('{"time_series": {', 'UTF-8')
# create counter. this is used to add the 'root' element during the first write, and to get rid of an extra comma after latitude in the final iteration.
counter = 0
# for every object in the time series
for number in time_series:
counter+=1
# if the site has recorded values
if(len(number['values'][0]['value']) > 0):
# pull out the data we want
json_data = bytes('"site": { "properties": { "usgs_name": ' + '"' + number['name'] + '",' + \
'"site_name": ' + '"' + number['sourceInfo']['siteName'] + '",' + \
'"huc": "{}"'.format(number['sourceInfo']['siteCode'][0]['value']) + ',' + \
'"streamflow": ' + '"' + number['values'][0]['value'][0]['value'] + '",' + \
'"dateTime": ' + '"' + number['values'][0]['value'][0]['dateTime'] + '",' + \
'"longitude": "{}"'.format(number['sourceInfo']['geoLocation']['geogLocation']['longitude']) + ',', 'UTF-8')
if counter < len(time_series) :
json_data = json_data + bytes('"latitude": "{}"'.format(number['sourceInfo']['geoLocation']['geogLocation']['latitude']) + '}},', 'UTF-8')
else:
json_data = json_data + bytes('"latitude": "{}"'.format(number['sourceInfo']['geoLocation']['geogLocation']['latitude']) + '}}', 'UTF-8')
# if it's the first iteration, prepend the 'root' of the json heirarchy
if(counter==1):
output.write(root + json_data)
else:
output.write(json_data)
# close the json object
output.write(bytes('}}', 'UTF-8'))
答案 0 :(得分:0)
请参阅代码with open('data/data.json', 'wb')
,这意味着对于每个请求,您都会打开文件并删除所有现有内容并撰写回复。
执行此操作的正确方法是with open('data/data.json', 'ab')
,当您将响应附加到现有文件而不删除它时。