我正在编写一个简单的脚本,用于收集地震数据并向我发送一条包含信息的文本。由于某些原因,我无法用新行分隔数据。我肯定我错过了一些简单的东西,但我对编程还是相当陌生,所以非常感谢任何帮助!下面是一些脚本:
import urllib.request
import json
from twilio.rest import Client
import twilio
events_list = []
def main():
#Site to pull quake json data
urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
webUrl = urllib.request.urlopen(urlData)
if (webUrl.getcode() == 200):
data = webUrl.read()
# Use the json module to load the string data into a dictionary
theJSON = json.loads(data)
# collect the events that only have a magnitude greater than 4
for i in theJSON["features"]:
if i["properties"]["mag"] >= 4.0:
events_list.append(("%2.1f" % i["properties"]["mag"], i["properties"]["place"]))
print(events_list)
# send with twilio
body = events_list
client = Client(account_sid, auth_token)
if len(events_list) > 0:
client.messages.create (
body = body,
to = my_phone_number,
from_ = twilio_phone_number
)
else:
print ("Received an error from server, cannot retrieve results " + str(webUrl.getcode()))
if __name__ == "__main__":
main()
要使用换行符拆分元组,需要调用
"\n".join()
函数。但是,您需要首先将元组中的所有元素转换为字符串以下表达式应适用于给定的元组:
请注意,这与将整个元组转换为字符串不同。相反,它迭代元组并将每个元素转换为自己的字符串
由于元组列表存储在“events_list”中,因此您可能可以执行以下操作:
它会给你这样的东西:
相关问题 更多 >
编程相关推荐