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__': b = [{'goodsId': '4773158314218656', 'goodsSpecId': '4773159127174358', 'goodsName': '展会5分钟方便面', 'goodsSpecName': '展会5分钟方便面+展会5分钟方便面', 'orderQuantity': 1, 'storeId': '5295107879530691'}] handler_yaml_list(b) 这段代码有什么错误
时间: 2023-09-08 12:10:00 浏览: 81
这段代码没有明显的错误,但是在运行代码时,没有输出结果,因为函数`handler_yaml_list`返回了一个新的字典,而没有将结果打印出来。可以将函数调用语句改为`print(handler_yaml_list(b))`,这样就能正确输出处理后的字典。完整的代码如下:
```
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__':
b = [{'goodsId': '4773158314218656', 'goodsSpecId': '4773159127174358', 'goodsName': '展会5分钟方便面',
'goodsSpecName': '展会5分钟方便面+展会5分钟方便面', 'orderQuantity': 1, 'storeId': '5295107879530691'}]
print(handler_yaml_list(b))
```
相关问题
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'
这个错误是因为`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))
```
阅读全文