在树莓派

时间:2019-03-08 17:01:33

标签: python json csv raspberry-pi

我正在调用DarkSky API,并将数据写入CSV。在我的Windows上,它按照数据键的顺序复制信息(键在尝试后位于注释行中)

在RPI上,它会复制数据,但会切换列的顺序。

例如:

在Windows上,csv的第一行是:时间,摘要,图标,最近的...

在RPI上,第一行是:precipIntensity,图标...

我在做什么错了?

代码:

import time
import requests
import csv
import json

try:

# 'time', 'summary', 'icon', 'nearestStormDistance', 'nearestStormBearing', 'precipIntensity', 'precipProbability', 'temperature', 'apparentTemperature', 'dewPoint', 'humidity', 'pressure', 'windSpeed', 'windGust', 'windBearing', 'cloudCover', 'uvIndex', 'visibility', 'ozone'

        while 1:
            URL =url
            r=requests.get(URL)
            data=r.json()

            csvrow=[]
            for k in data['currently'].keys():
                if k=='time':
                    csvrow.append(time.strftime("%D %H:%M", time.localtime(int(data['currently']['time']))))
                else:
                    csvrow.append(data['currently'][k])

            with open('Weather Project CSV', mode='a') as wpj:
                wpj = csv.writer(wpj, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                wpj.writerow(csvrow)
            time.sleep(10)
except:
    print('Error Encountered')
    #turn on LED

输出:

RPI第一行:

Clear,-0.31,3.11,clear-day,111,15.27,230,03/08/19 11:49,435.84,-3.97,257,3,0,-12.06,0.23,1027.93,0,4.65,0.41

Windows第一行:

03/08/19 11:58,Partly Cloudy,partly-cloudy-day,109,256,0,0,-0.19,-3.84,-11.91,0.41,1027.76,3.13,4.72,229,0.27,3,15.13,434.52

0 个答案:

没有答案