一. 有两行数据,存在test_data.txt:
param:{"phone":"18688773467","pwd":"123456"}@url:"http://119.23.241.154:8080/futureloan/mvc/api/member/register"
param:{"phone":"18688773467","pwd":"123456"}@url:"http://119.23.241.154:8080/futureloan/mvc/api/member/login"
请利用open函数把这两行数据读取出来,进行数据处理,然后最终的结果是:
res=[{‘param‘:{"phone":"18688773467","pwd":"123456"},‘url‘:‘http://119.23.241.154:8080/futureloan/mvc/api/member/register‘},{‘param‘:{"phone":"18688773467","pwd":"123456"},‘url‘:‘http://119.23.241.154:8080/futureloan/mvc/api/member/login‘} ]
def read_file(f): res = [] file = open(f,‘r‘) content = file.readlines() #读取所有行 for line in content: temp = line.strip("\n") #处理行末尾的换行符 temp_1 = temp.split("@") #按@再划分 d = {} for item in temp_1: sep = item.find(":") k = item.split(":",1)[0] v = item.split(":",1)[1] d[k]=v #组合为字典 res.append(d) file.close() return res res = read_file("test_data.txt") print("res={}".format(res))
原文地址:https://www.cnblogs.com/Elaine1/p/10434506.html
时间: 2024-11-07 14:09:54