【发布时间】:2021-01-19 07:41:22
【问题描述】:
我有一个每 2 秒运行一次的代码。此代码每两秒打印一次坐标信息。我想在列表中收集这些坐标,但我不能。我该怎么做?
代码:
import time
import requests
import schedule
def executeSomething():
r = requests.get('https://get.geojs.io/')
ip_request = requests.get("https://get.geojs.io/v1/ip.json")
ippAdd = ip_request.json()["ip"]
url = 'https://get.geojs.io/v1/ip/geo/' + ippAdd + '.json'
geo_request = requests.get(url)
geo_data = geo_request.json()
co=[]
co.append([float(geo_data["latitude"]),float(geo_data["longitude"])])
print(co)
schedule.every(2).seconds.do(executeSomething)#This code run every 10 seconds
#schedule.every().hour.do(executeSomething())
while 1:
schedule.run_pending()
time.sleep(1)
输出:
[[39.9208, 32.8375]]
[[39.7856, 32.2174]]
但我想要这样的输出:
[[39.9208, 32.8375], [39.7856, 32.2174]]
编辑: 我还有一个问题。当把print(co)改成return co并把这个函数导入另一个代码并尝试获取“co”列表时,我无法获取。
import dynamic
d = dynamic.executeSomething()
print(d)
我做错了什么?
【问题讨论】:
-
修正你的身份。
-
我修正了标识。
-
您在每次运行时将
co重置为函数内的空列表。