Python 编程,open data with Pandas,open data在那条URL链接中,详细在图里,求代码,急用

(如图所示,内容以英文为准。图1是任务内容和示例答案,图2是任务内容的中文翻译,图3-4是已提供一部分的code,图5是URL链接(open data在里面),)

这个程序完成了,程序以这一版为准.

最后一部分主函数main部分,因为没有搭环境,没有测试,

Q1,Q2,Q3部分都测试完毕,如果你有测试环境可以测试一下Q4和主函数部分.

Q4只有一条语句Report(arg,nTemp,nRain,sDate,sTime)

注意Q1部分在Your code第1行加了一句weather=jdata

(见图,注意图中源代码的缩进)

import requests

import pandas as pd

import json

import sys,getopt

dUpdate=""

weather=pd.DataFrame({"A":[]})

#%%

def getData():

global dUpdate,weather

url="https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=rhrread&lang=en"

response=requests.get(url)

data=response.text

jdata=json.loads(data)

dTemp=jdata['temperature']['data']

dRain=jdata['rainfall']['data']

dUpdate=jdata['updateTime']

dfTemp=pd.DataFrame(dTemp)

dfRain=pd.DataFrame(dRain)

### Your code

weather=jdata

dfTemp.rename({"unit":"unit_x"},axis="columns",inplace=True)

dfRain.rename({"unit":"unit_y"},axis="columns",inplace=True)

dt=pd.merge(dfTemp,dfRain,how='outer',sort=True)

new_columns=['place','unit_y','max','main','value','unit_x']

dt=dt.reindex(columns=new_columns)

dt.rename({"place":""},axis="columns",inplace=True)

dt.rename({"unit_x":"unit"},axis="columns",inplace=True)

dt.rename({"unit_y":"unit"},axis="columns",inplace=True)

dt.iloc[:, 0] = (lambda s: s.str.ljust(s.str.len().max()))(dt.iloc[:, 0])

dt=dt.to_string(index = False)

print(dt)

### Q1

###

#%%

def getDateTime(sDateTime):

sDate=sDateTime.split("T")[0]

sTime=sDateTime.split("T")[1].split("+")[0]

return sDate,sTime

def getTempRain(Region,weather):

nTemp=-1.0

nRain=-1.0

### Your Code

for i in weather['temperature']['data']:

if Region==i['place']: 

nTemp=i['value']

for i in weather['rainfall']['data']:

if Region==i['place']: 

nRain=i['max']

### Q2

###

return nTemp,nRain

#%%

def Report(Place,cTemp,cRain,cDate,cTime):

print("---------------------------------------------------")

print("Current Weather Summary on {} at {}".format(cDate,cTime))

print("Location:{}".format(Place))

### Your code

if int(cTemp)==-1 and int(cRain)==-1:

print("no record for Region")

else:

if int(cTemp)==-1:

pass

else:

print("Current Temperature: %.1f C" % cTemp)

if int(cRain)==-1:

pass

else:

if cRain<0.05:

print("No rainfall for the last hour")

else:

print("Rainfall for the last hour: %.1f mm" % cRain)

### Q3

###

print("-----------------End of Report---------------------")

#%%

mHELP='weather.py -r Region'

def main(argv):

try:

opts,args=getopt.getopt(argv,"r:")

except getopt.GetoptError:

print(mHELP)

sys.exit(2)

for opt,arg in opts:

if opt=='-r':

sDate,sTime=getDateTime(dUpdate)

nTemp,nRain=getTempRain(arg,weather)

### Your Code

Report(arg,nTemp,nRain,sDate,sTime)

### Q4

###

sys.exit()

print(mHELP)

# LastReport()

if (__name__=="__main__"):

getData()

main(sys.argv[1:])

else:

## Unit Test for Q1,Q2 and Q3

print("Testing")

getData()

追问

请问Q4要打什么指令去测试?

追答

应该是用python3 weather.py -r "Tai Po" 命令测试,
我这里没搭环境,麻烦你测试一下.

温馨提示:答案为网友推荐,仅供参考