一个诞生于和盆友聊天时的Python 数据转换需求

2020-08-20 09:55:59,好盆友给我发了条微信,内容如下:

通过和盆友的微信交流,我get到了她的想法,知道了她最终想要的数据格式。于是就有了今天的分享内容。

我分4 步去实现了这个来自盆友的小需求~

1、格式化数据

去掉多余行、空格、换行符

 1 import json
 2 origin_result_data = [b'*************************** 1. row ***************************
', 
 3                       b'id: 1
', 
 4                       b' param_name: enterprise
', 
 5                       b'param_value: {"name": "", "abbreviation": "", "description": " EditByHS form APIs", "logo_path": ""}
', 
 6                       b'description:  
', 
 7                       b'      level: 1
', 
 8                       b'update_time: 1970-01-01 00:00:00.000000
', 
 9                       b'  parent_id: NULL
', 
10                       b'*************************** 2. row ***************************
', 
11                       b'         id: 2
', 
12                       b' param_name: init_password
', 
13                       b'param_value: 0
', 
14                       b'description:   
', 
15                       b'      level: 1
', 
16                       b'update_time: 0000-00-00 00:00:00.000000
', 
17                       b'  parent_id: NULL
', 
18                       b'*************************** 3. row ***************************
', 
19                       b'         id: 3
', 
20                       b' param_name: clear_log_info
', 
21                       b'param_value: {"set_time": "2020-08-14", "clear_interval": 0}
', 
22                       b'description:  
', 
23                       b'      level: 1
', 
24                       b'update_time: 0000-00-00 00:00:00.000000
', 
25                       b'  parent_id: NULL
']
26 #去除
、空格字符
27 s = [x.strip() for x in origin_result_data if x.strip() != '']
28 for i in s:
29     if "****" in str(i):
30         #去除字符串中包含***的字符串,即删除掉每一条数据的第一行
31         s.remove(i)

2、大列表分割成小列表 

将大列表有序分割成小列表

 1 def list_of_groups(list_info, per_list_len):
 2     """
 3     :param list_info:   列表
 4     :param per_list_len:  每个小列表的长度
 5     :return:
 6     """
 7     list_of_group = zip(*(iter(list_info),) * per_list_len)
 8     end_list = [list(i) for i in list_of_group] # i is a tuple
 9     count = len(list_info) % per_list_len
10     end_list.append(list_info[-count:]) if count !=0 else end_list
11     return end_list
12  
13  
14 def test_ret():
15     list_info = s
16     #将大列表中每7个元素组成一个小列表
17     ret = list_of_groups(list_info, 7)
18     return ret

3、列表与字典层层嵌套 

大列表中嵌套字典,字典中再次嵌套字典

 1 target_result_data = []
 2 for item in test_ret():
 3     data = {}
 4     data['id'] = str(item[0]).split(": ")[1].split("'")[0]
 5     data['param_name'] = str(item[1]).split(': ')[1].split("'")[0]
 6     if str(item[2]).split(": ")[1].split("'")[0] == '0':
 7         data['param_value'] = 0
 8     else:
 9         data['param_value'] = "{" + str(item[2]).split(": {")[1].split("'")[0]
10         data['param_value'] = json.loads(data['param_value'])
11     data['description'] = str(item[3]).split(':')[1].split("'")[0]
12     data['level'] = str(item[4]).split(': ')[1].split("'")[0]
13     data['update_time'] = str(item[5]).split(': ')[1].split("'")[0]
14     data['parent_id'] = str(item[6]).split(': ')[1].split("'")[0]
15     target_result_data.append(data)
16 print(target_result_data)

4、满足需求结果输出

输出盆友想要的数据格式结果

 1 target_result_data = [
 2     {'id': '1',
 3      'param_name': 'enterprise',
 4      'param_value': {'name': '', 'abbreviation': '', 'description': ' EditByHS form APIs', 'logo_path': ''},
 5      'description': '',
 6      'level': '1',
 7      'update_time': '1970-01-01 00:00:00.000000',
 8      'parent_id': 'NULL'
 9      },
10  
11     {'id': '2',
12      'param_name': 'init_password',
13      'param_value': 0,
14      'description': '',
15      'level': '1',
16      'update_time': '0000-00-00 00:00:00.000000',
17      'parent_id': 'NULL'
18      },
19  
20     {'id': '3',
21      'param_name': 'clear_log_info',
22      'param_value': {'set_time': '2020-08-14', 'clear_interval': 0},
23      'description': '',
24      'level': '1',
25      'update_time': '0000-00-00 00:00:00.000000',
26      'parent_id': 'NULL'
27      }
28 ]

欢迎关注【无量测试之道】公众号,回复【领取资源】,
Python编程学习资源干货、
Python+Appium框架APP的UI自动化、
Python+Selenium框架Web的UI自动化、
Python+Unittest框架API自动化、

资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。

备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:

 添加关注,让我们一起共同成长!

原文地址:https://www.cnblogs.com/Wu13241454771/p/13595833.html