0

我需要能够从函数中的文件中获取 2 个值。然后在字典中返回两者。这就是我到目前为止所拥有的

def getWeatherData():
    f = open("/Users/kamal/Desktop/text.html",'r')
    myfile = f.read()
    w = myfile.find('''<tr><th colspan="15" class="wxo-th-bkg table-date">''')

    temperature = myfile[w+494:w+496]
    temp = temperature.strip()
    cloudyness = myfile[w+396:w+415]
    cloud = cloudyness.strip()
    cloud = cloud.replace("</p></div","")
    print(temp,cloud)
    dictionnary = {"temp":str(temp),"Condition":str(cloud)}
    f.close()
    return dictionnary

getWeatherData()
4

2 回答 2

2
dictionnary = getWeatherData()

之后您可以打印或访问dictionnary

于 2020-03-10T19:00:20.187 回答
2

如果您希望多次调用函数以从文件中查找不同的值,则需要在函数中引入一个参数,您可以使用该参数在文件中查找位置。例如:

def getWeatherData(location):
   # your code

如果要将字典分配给变量,则需要使用

table = getWeatherData()
于 2020-03-10T18:45:15.050 回答