您是否安装了最新版本的 PyOwm 并遇到以下问题
<code class="hljs language-python">'OWM' object has no attribute 'weather_at_place'</code>
? 如果是,请卸载它,并使用以下命令安装以前的版本
- pip install pyowm==2.10.0
2.54/5 (5投票s)
我们使用带有 OpenWeatherMap (PyOWM) 的命令行界面,以便在 Python 中更轻松地使用 OpenWeatherMap API。
在这个例子中,我们将一个嵌入的 Python 脚本与 Pascal 脚本结合起来。我们将使用 py-script 语言和一个名为 Python OpenWeatherMap (PyOWM) 的出色库,以便在 Python 中更轻松地使用 OpenWeatherMap
API。您需要先 pip 安装该库才能导入该模块。
在 Windows 上,标准的 Python 安装程序已经将 .py 扩展名与文件类型(Python.File)相关联,并为该文件类型提供了一个运行解释器的打开命令(F:\Program Files\Python\python.exe “%1” %*)。这足以使脚本可以从命令行提示符执行。 我们使用 python-dll,因为它是一个 Windows DLL。
因此 *.pyd 文件是 dll,但有一些区别
到目前为止,您需要了解 3 种不同的文件类型
然后你必须安装 PyOWM
库
C:\maXbox>pip3 install pyowm
Collecting pyowm
Downloading pyowm-2.10.0-py3-none-any.whl (3.7 MB)
您是否安装了最新版本的 PyOwm 并遇到以下问题
<code class="hljs language-python">'OWM' object has no attribute 'weather_at_place'</code>
? 如果是,请卸载它,并使用以下命令安装以前的版本
然后,我们使用像命令行界面一样的参数运行一个命令提示符命令 ('py
'+RUNSCRIPT
),并通过函数 getDosOutput
在 maXbox 中获得 Python 输出。
Python 脚本名为 '1046_openweather.py',我们使用 writeln(getDosOutput('py '+RUNSCRIPT+' '+outputpath+' '+locate, exePath));
调用它。 你可以在 P4D 中经常看到这一点(稍后),脚本本身从表单中的备忘录控件运行,或者使其更全面,就像一个内联 const
文本(脚本),通过 DOS 输出返回到 memo2
中,如下所示
program OpenWeatherMap_Py_integrate;
const C = CRLF;
const SCRIPTNAMEP = '1046_openweather.py';
const DETECTOUTFILE = 'openweather_out2.txt';
Const PYSCRIPT6 =
'import pyowm '+C+
'import wget '+C+
'import sys, datetime as dt '+C+
'#nltk.download("vader_lexicon") '+C+
'from time import sleep '+C+
'import pandas as pd '+C+
'pd.set_option("max_colwidth", 400) '+C+
'import numpy as np '+C+
'print("this first line after config & imports") '+C+
' '+C+
'output_path = sys.argv[1] '+C+
'locate = sys.argv[2] '+C+
' '+C+
'owm= pyowm.OWM("55013bf3d09cfb0619989a00ed5bed09") '+C+
'observation= owm.weather_at_place((locate)) '+C+
'we= observation.get_weather() '+C+
'temp = we.get_temperature("celsius") '+C+
'with open(output_path, "w") as file: '+C+
' file.write("OpenWeatherMap of "+locate+" "+str(dt.datetime.now())+ '+C+
' "\n"+str(we)+ '+C+
' "\n"+str(temp)+ '+C+
' "\n"+str(dt.datetime.utcnow())) '+C+
' '+C+
'print("\n") '+C+
'print("weather today:"+(locate)+" "+str(we)+"\n"+str(temp)) '+C+
'print("integrate weatherreport detector compute ends...") ';
Const ACTIVESCRIPT = PYSCRIPT6;
var RUNSCRIPT, outputPath, locate: string;
startTime64, endTime64, freq64: Int64;
begin //@main
//-init env
maxform1.console1click(self);
memo2.height:= 205;
QueryPerformanceFrequency(freq64);
//-config
saveString(exepath+SCRIPTNAMEP, ACTIVESCRIPT);
sleep(600)
//outputPath:= '.\crypt\output\'+DETECTOUTFILE;
if Not fileExists(exepath+DETECTOUTFILE) then
CreateFileFromString(exepath+DETECTOUTFILE, 'Open_Weather_Data');
outputPath:= Exepath+DETECTOUTFILE;
locate:= '"Bern, CH"';
if fileExists(exepath+SCRIPTNAMEP) then begin
RUNSCRIPT:= exepath+SCRIPTNAMEP;
QueryPerformanceCounter(startTime64);
writeln(getDosOutput('py '+RUNSCRIPT+' '+outputpath+' '+locate, exePath));
QueryPerformanceCounter(endTime64);
println('elapsedSeconds:= '+floattostr((endTime64-startTime64)/freq64));
openFile(outputPath)
end;
end;
end.
然后,我们使用 API 密钥进行身份验证。 与 API 的经过身份验证的连接存储在 owm_obj 对象中。 您将必须提供您在 Open Weather Map 网站注册后获得的 API 密钥。
请使用另一个 API 密钥:owm= pyowm.OWM(" <Your Enter_API_KEY> ")
默认情况下,您获得的 API 密钥是免费的。 但是,您可以选择付费密钥,该密钥具有其他权限。
要获取特定城市的当前天气,您需要通过提供城市名称、id 或坐标,使用 omw()
对象创建一个观察对象。 要使用城市名称获取观察对象,请使用 weather_at_place()
方法。
然后,我们使用像命令行界面一样的参数运行一个命令提示符命令(‘py ‘+RUNSCRIPT
),并通过函数 getDosOutput()
在 maXbox 中获得 Python 输出。
此外,我们将天气数据保存在一个名为 'openweather_out2.txt' 的文件中。
您可以在以下位置找到该脚本
http://www.softwareschule.ch/examples/openweather.txt.
然后我们使用脚本中的参数作为 paramstrings
。 ParamStr
函数返回用于调用当前脚本的命令行中的一个参数,其中包含文件的 outputpath 和 locate,这意味着返回天气报告的地点(伯尔尼、巴塞尔、科隆、杜塞尔多夫、基律纳等)(ParamIndex
确定返回哪个参数)
Writeln(getDosOutput(‘py ‘+RUNSCRIPT+’ ‘+outputpath+’ ‘+locate, exePath));
weather today:Bern, CH <pyowm.weatherapi25.weather.Weather –
reference time=2021-07-07 14:43:23+00, status=clouds, detailed status=scattered clouds>
{‘temp’: 21.17, ‘temp_max’: 23.54, ‘temp_min’: 15.99, ‘temp_kf’: None}
integrate weatherreport detector compute ends…
此控制台输出带有预测术语 rain:True
OpenWeatherMap of Vienna, AT 2021-07-15 09:26:46.302472
<pyowm.weatherapi25.weather.Weather - reference time=2021-07-15 07:26:45+00, status=clear, detailed status=clear sky>
{'temp': 21.55, 'temp_max': 23.75, 'temp_min': 19.88, 'temp_kf': None}
rain:True
UTC:2021-07-15 07:26:46.302472
注意:您需要相应地调整演示,才能成功加载您安装在计算机上的 Python 发行版。 Python 的优势之一是它默认包含电池,该库使用 sys.argv[0]
元素的值来设置程序名称,正如您可能已经知道的那样,它是您执行的 Python 脚本的名称。 然后我们使用参数向量 1 和 2 用于我们的命令行界面
我们使用 'output_path = sys.argv[1] 和 'locate = sys.argv[2]
'。