for result in results: tracking_number = result[0] # 调用 query_logistics_trajectory 函数查询物流轨迹 logistics_trajectory = query_logistics_trajectory(tracking_number) # 将 list 转换为 JSON 字符串 if() logistics_trajectory_json = json.dumps(logistics_trajectory['result']['list']) # 更新表 logistics_trajectory 中的 logistics_trajectory 字段 sql = f"UPDATE logistics_trajectory SET logistics_trajectory='{logistics_trajectory_json}' WHERE tracking_number='{tracking_number}' AND state='1'" cursor.execute(sql) 以下logistics_trajectory['result']['list']代码如何更改防止报错
时间: 2024-03-24 14:38:04 浏览: 61
trace.zip_Trajectory tracking_sliding_机械手_滑模 轨迹_轨迹跟踪
要防止报错,需要加入判断语句,确保 logistics_trajectory['result']['list'] 存在且不为空。
可以使用以下代码进行更改:
```python
if 'result' in logistics_trajectory and 'list' in logistics_trajectory['result'] and logistics_trajectory['result']['list']:
logistics_trajectory_json = json.dumps(logistics_trajectory['result']['list'])
else:
logistics_trajectory_json = None
```
这样,如果 logistics_trajectory['result']['list'] 不存在或为空,logistics_trajectory_json 就会被赋值为 None,避免了对空值进行操作导致的报错。
阅读全文