def handler_yaml_list(data_dict): """处理yaml文件测试用例请求参数为list情况,以数组形式""" for key, value in data_dict.items(): if isinstance(value, list): value_lst = ','.join(value).split(',') data_dict[key] = value_lst return data_dict if __name__ == '__main__': a = '[{"goodsId": "4773158314218656", "goodsSpecId": "4773159127174358", "goodsName": "展会5分钟方便面", "goodsSpecName": "展会5分钟方便面+展会5分钟方便面", "orderQuantity": 1, "storeId": "5295107879530691"}]' b = json.loads(a) print(handler_yaml_list(b)) python报错'list' object has no attribute 'items'
时间: 2023-07-19 17:52:32 浏览: 80
iic_int_handler.rar_2368_IIC_INT_Handler_LPC IIC_lpc2368_lpc2368
这个错误是因为`b`是一个列表,而不是字典,无法使用`items()`方法。需要将列表中的字典取出来再处理。可以将`handler_yaml_list`函数改成如下形式:
```
def handler_yaml_list(data_list):
"""处理yaml文件测试用例请求参数为list情况,以数组形式"""
for data_dict in data_list:
for key, value in data_dict.items():
if isinstance(value, list):
value_lst = ','.join(value).split(',')
data_dict[key] = value_lst
return data_list
```
同时,调用函数时应该传入`b`而不是`json.loads(a)`。完整的代码如下:
```
import json
def handler_yaml_list(data_list):
"""处理yaml文件测试用例请求参数为list情况,以数组形式"""
for data_dict in data_list:
for key, value in data_dict.items():
if isinstance(value, list):
value_lst = ','.join(value).split(',')
data_dict[key] = value_lst
return data_list
if __name__ == '__main__':
a = '[{"goodsId": "4773158314218656", "goodsSpecId": "4773159127174358", "goodsName": "展会5分钟方便面", "goodsSpecName": "展会5分钟方便面+展会5分钟方便面", "orderQuantity": 1, "storeId": "5295107879530691"}]'
b = json.loads(a)
print(handler_yaml_list(b))
```
阅读全文