|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
txt文件中的内容为:
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
1、将文件的数据读取出来,并根据逗号交其分隔出来 ;
2、将分隔出来的数据,再以冒号进行分隔。冒号之前为key,冒号之后为value。
3、最终结果要求如下:操作文件最终结果要求如下:
[{"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"}]
本帖最后由 ABC23 于 2018-4-25 01:45 编辑
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
[{"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"}]
=======================================================================
首先分析文本特点,一共两行。readline、readlines一次读取一行。
data = []
with open('demo.txt', 'r') as fp:
for line in fp.readlines():
dikt = {}
lyst = line.strip().split(',')
for item in lyst:
dikt[item.split(":", 1)[0]] = item.split(":", 1)[1]
data.append(dikt)
with open('demo.txt', 'w') as fp:
fp.write(str(data))
============================================
split方法如果不支持正则(应该是支持的),就用re模块:re.sub()进行分割。
|
|