读取txt文件中的数据

在 一个data.txt文件中,有如下格式的数据:

url:http://www.weather/api/city/signup,mobilephone:13060246701,pwd:123456

url:http://www.weather/api/city/signin,mobilephone:15678934552,pwd:234555

需要将其读取出来,并进行整理成下列格式:

[{'url': 'http://www.weather/api/city/signup', 'mobilephone': '13060246701', 'pwd': '123456'},

{'url': 'http://www.weather/api/city/signin', 'mobilephone': '15678934552', 'pwd': '234555'}]


代理整理如下. 

def get_data_out(file_path):

    file =open(file_path,'r',encoding="utf-8")

    lis = []

    for fin file:

        dic = {}

        sb = f.strip().split(',')

        for sin sb:

            sub = s.split(':',1)

            dic[sub[0]] = sub[1]

       lis.append(dic)

    file.close()

    return lis

if __name__ =='__main__':

    file_path ="data.txt"

    lis = get_data_out(file_path)

    print(lis)

你可能感兴趣的:(读取txt文件中的数据)