目标:将输出更改为仅打印纬度和经度。我也想添加这两个字符串,但是我知道怎么做。我正在苦读这本字典。我试着通过json把它传递给string,但是我一直收到错误。
代码:
import asyncio
import aiohttp
from pytraccar.api import API
HOST = "x"
PORT = x
USERNAME = "x"
PASSWORD = "x"
async def test():
async with aiohttp.ClientSession() as session:
data = API(LOOP, session, USERNAME, PASSWORD, HOST, PORT)
await data.get_device_info()
test55 = (data.positions)
print(test55)
LOOP = asyncio.get_event_loop()
LOOP.run_until_complete(test())输出:
[{'id': 55555, 'attributes': {'batteryLevel': 49.0, 'distance': 0.0, 'totalDistance': 866122.19, 'motion': False}, 'deviceId': 1, 'type': None, 'protocol': 'osmand', 'serverTime': '2020-06-19T15:25:58.160+0000', 'deviceTime': '2020-06-19T16:21:01.000+0000', 'fixTime': '2020-06-19T16:21:01.000+0000', 'outdated': False, 'valid': True, 'latitude': 39.204066, 'longitude': -71.677783, 'altitude': 41.93764706884086, 'speed': 0.0, 'course': 0.0, 'address': None, 'accuracy': 10.0, 'network': None}]编辑:我马上就到了:
pull1 = json.dumps(test55).split(',')
print(pull1[5])输出:
"deviceId": 1现在,也许我要做的就是替换设备ID。
编辑:感谢大家的帮助。这就是我最后做的:
pull = json.dumps(test55).split(',')
print(pull[10][12:]) #fix time
print(pull[13][12:]) #lat
print(pull[14][13:]) #long
print(pull[15][12:]) #alt
print(pull[19][12:]) #accuracy发布于 2020-06-20 20:39:13
更新
我认为这段split-dictionary和其余代码的代码应该是正确的
if __name__ == "__main__":
dict = [{'id': 55555, 'attributes': {'batteryLevel': 49.0, 'distance': 0.0, 'totalDistance': 866122.19, 'motion': False}, 'deviceId': 1, 'type': None, 'protocol': 'osmand', 'serverTime': '2020-06-19T15:25:58.160+0000', 'deviceTime': '2020-06-19T16:21:01.000+0000', 'fixTime': '2020-06-19T16:21:01.000+0000', 'outdated': False, 'valid': True, 'latitude': 39.204066, 'longitude': -71.677783, 'altitude': 41.93764706884086, 'speed': 0.0, 'course': 0.0, 'address': None, 'accuracy': 10.0, 'network': None}]
print(dict)
print(dict[0]['latitude'])
print(dict[0]['longitude'])发布于 2020-06-20 21:17:58
答案:
pull = json.dumps(test55).split(',')
print(pull[10][12:]) #fix time
print(pull[13][12:]) #lat
print(pull[14][13:]) #long
print(pull[15][12:]) #alt
print(pull[19][12:]) #accuracy我稍后会将其作为字符串传递,并将调用发送到Google sheets。
https://stackoverflow.com/questions/62474622
复制相似问题