使用python写从文件MockServer.log读取{"MsgType": 4002, "MsgSequence": 978, "DeviceSN": "qxwz0007", "Timestamp": 1678177163410, "CoordinateSystem": 1, "PtcSize": 21, "PtcList": [ {"Id": 1233, "Timestamp": 1678177163410, "IsTracked": 2, "Type": 1, "PlateNum": "None", "Length": 490.0, "Width": 180, "Height": 145, "Lon": 120.4489336, "Lat": 31.600737, "Ele": 12.9073119, "XPos": 58.6922748, "YPos": 23.1289603, "PosConfidence": 8, "Speed": 0.0722669, "Heading": 14.3506278, "VehType": 10, "VehColor": -1, "EnterLane": -1, "ExportLane": -1, "Lane": 0, "ToStop": -1, "CrossName": ""}]}循环读取该文件中数据中所有id的值
时间: 2023-03-31 12:04:29 浏览: 162
用python实现文件的读取
可以使用以下代码实现:
```python
import json
with open('MockServer.log', 'r') as f:
data = f.readlines()
for line in data:
try:
json_data = json.loads(line)
if json_data['MsgType'] == 4002:
for ptc in json_data['PtcList']:
print(ptc['Id'])
except:
continue
```
这段代码会循环读取文件中的每一行数据,将其转换为 JSON 格式,并判断是否为 MsgType 为 4002 的数据。如果是,则遍历其中的 PtcList,输出每个 Ptc 的 Id 值。
阅读全文