param:{"phone":"18688773467","pwd":"123456"}@url:"http://119.23.241.154:8080/futureloan/mvc/api/member/register"api
param:{"phone":"18688773467","pwd":"123456"}@url:"http://119.23.241.154:8080/futureloan/mvc/api/member/login"mvc
請利用open函數把這兩行數據讀取出來,進行數據處理,而後最終的結果是:app
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))