1、从输入的字典中获取给定key的值,
实现函数:get_key_value(source_dict, key, index=0),source_dict指给定的字典,key指需要获取的key字段,index指的是需要获取相同key的第几个值,从0开始,默认是0。
举例:
现有字典:
a = {
"code":200,
"msg":"success",
"data":{
"list":{
"pay_column":[
],
"column":[
{
"id":6443650,
"app_id":1561979372,
"username":"qfhyadx",
"alias":"",
"title":"\u5de5\u5177",
"desc":"",
"img_url":"https:\/\/img-blog.csdnimg.cn\/20190927151132530.png",
"channel_id":0,
"sum":16,
"sort":6443650,
"is_show":1,
"status":1,
"is_free":0,
"is_delete":2,
"status_count":"{\"1\":16}",
"created_at":"2019-08-26T04:19:51Z",
"updated_at":"2019-11-22T07:53:45Z",
"second":false,
"edit_title":"\u5de5\u5177",
"column_subscribe_count":0,
"column_url":"https:\/\/blog.csdn.net\/qfhyadx\/category_6443650.html",
"is_audit_pending":false
},
{
"id":6459549,
"app_id":1561979372,
"username":"qfhyadx",
"alias":"",
"title":"\u968f\u7b14",
"desc":"",
"img_url":"https:\/\/img-blog.csdnimg.cn\/20190927151043371.png",
"channel_id":0,
"sum":4,
"sort":6459549,
"is_show":1,
"status":1,
"is_free":0,
"is_delete":2,
"status_count":"{\"1\":4}",
"created_at":"2019-08-26T04:19:51Z",
"updated_at":"2020-07-19T21:49:55Z",
"second":false,
"edit_title":"\u968f\u7b14",
"column_subscribe_count":0,
"column_url":"https:\/\/blog.csdn.net\/qfhyadx\/category_6459549.html",
"is_audit_pending":false
},
{
"id":7554611,
"app_id":1561979372,
"username":"qfhyadx",
"alias":"",
"title":"python3",
"desc":"",
"img_url":"https:\/\/img-blog.csdnimg.cn\/20190927151101105.png",
"channel_id":0,
"sum":2,
"sort":7554611,
"is_show":1,
"status":1,
"is_free":0,
"is_delete":2,
"status_count":"{\"1\":2}",
"created_at":"2019-08-26T04:20:06Z",
"updated_at":"2020-07-13T19:41:03Z",
"second":false,
"edit_title":"python3",
"column_subscribe_count":0,
"column_url":"https:\/\/blog.csdn.net\/qfhyadx\/category_7554611.html",
"is_audit_pending":false
}]
},
"total":3,
"un_pass_total":0,
"recycle_total":0
}
}
调用函数:get_key_value(a, ‘id’, 2),返回值应该是:7554611
2、从文件中获取指定字符所在行数据。实现函数:check_string(file_path, key_word),file_path代表文件所在路径,key_word代码搜索的关键字,返回符合条件的列表
现有文件:a.log,文件内容如下:
update starting
Mysql update starting....
Mysql update complete
AuthServer update starting....
AuthServer update complate
NetWorkServer update starting...
NetWorkServer update complate
GDPSServer update starting...
GDPSServer update complate
[update result] successful...
现在进行如下调用:check_string(‘D:\a.log’,‘update result’),则返回:[‘[update result] successful…’]。如果进行如下调用:check_string(‘D:\a.log’,‘complate’),则返回:[‘Mysql update complete’, ‘AuthServer update complate’, ‘NetWorkServer update complate’, ‘GDPSServer update complate’]