数据示例:
url:http://119.23.241.154:8080/futureloan/mvc/api/member/login,mobilephone:13760246701,pwd:123456
url:http://119.23.241.154:8080/futureloan/mvc/api/member/login,mobilephone:15678934551,pwd:234555
将数据保存到文本格式文件中,以下示例的文件名为test3
def read_data(test_data):
text_list = []
with open(test_data) as file:
# 读取多行数据,组成一个list,并且遍历这个list,得到str格式的每行请求数据
for text in file.readlines():
text_dict = {}
# 将每行请求的换行符去除,并用逗号分割各子模块,然后遍历子模块。
# 字符串strip后还是一个字符串,可以继续split分割,split后产生一个列表
for text1 in text.strip("\n").split(","):
# split后可接参数,只分割1次,解决url内部分号问题,返回的是各子模块,格式是列表
text2 = text1.split(":", 1)
# 将子模块的第一个值作为key,第二个值作为value,存入字典
text_dict[text2[0]] = text2[1]
# 每个请求转换成字典后再加入目标列表
text_list.append(text_dict)
print(text_list)
read_data("test3")
运行结果:
[{'url': 'http://119.23.241.154:8080/futureloan/mvc/api/member/login', 'mobilephone': '13760246701', 'pwd': '123456'}, {'url': 'http://119.23.241.154:8080/futureloan/mvc/api/member/login', 'mobilephone': '15678934551', 'pwd': '234555'}]