如何解决迭代字符串的一部分
我想提取一些具有以下代码的数据:
import http.client
conn = http.etc("something")
headers = {
'id': "asdfghjk",'accept': "application/json"
}
conn.request("GET","/SomeLocationData?latitude=50&longitude=10&time=631152000",headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
现在,我想提取所有从纬度45开始到55结束的数据,对于经度和时间也是如此。因此,对于每一个我都有一个范围,我需要所有可能的组合。如何为该字符串内的每个值编写一个for循环?
编辑:这是我用来从服务中提取数据的代码,我只输入纬度,经度和时间的值。输出类似于:
[{"contentVersion":1,"location":{"latitude":48.003,"longitude":15.998001,"time":1597287600},"precipitation":-999,"symbolCode":-999,"temperature":15,"windDirection":190.22656,"windSpeed":0.015625}]
解决方法
每个人都说,这取决于您从GET方法获取的数据,为了更好地理解,我现在为下面的示例JSON显示该信息。
让我们考虑这是从GET方法获得的输出。
[
{
"contentVersion": 1,"location": {
"latitude": 48.003,"longitude": 15.998001,"time": 1597287600
},"precipitation": -999,"symbolCode": -999,"temperature": 15,"windDirection": 190.22656,"windSpeed": 0.015625
},{
"contentVersion": 2,"location": {
"latitude": 58.003,"longitude": 5.998001,"time": 1297287600
},{
"contentVersion": 1,"location": {
"latitude": 40.003,"longitude": 51.998001,"time": 1097287600
},"windSpeed": 0.015625
}
]
下面是我们可以获取相应值的代码。
for x in range(len(data)):
lati = int(data[x]['location']['latitude'])
# Getting Latitude on a range
if lati > 45 and lati > 50:
print(lati)
# Same way for the other two
对于使用上述JSON的上述代码,Latitude的输出将仅为58。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。