#第1题
#encoding=utf-8
"""
数据文件:data.log
20160215000148|0|collect info job pos|success|
20160215000153|0|collect info job end|success|resultcode = 0000
20160216000120|0|collect info job pos|success|
20160216000121|0|collect info job end|success|resultcode = 0000
20160217000139|0|collect info job pos|success|
20160217000143|0|collect info job end|success|resultcode = 0000
数据分析需求:
每行内容需要生成以每行首年月日为名称的文件,文件内容写入|0|后的所有行内容(也包括|0|)
"""
#算法:1.把所有内容放到一个列表中
#2.把每行的年月日取出做文件的名字
#3.把/0/和之后的字符串写到对应日期的文件里面
#4.循环的执行以上步骤
def dellWithData():
tempList=[]
with open("e:\\murphy\\data.txt","r",encoding="utf8") as fp:
tempList=fp.readlines()
for i in tempList:
if i[:8]=="20160215":
with open("e:\\murphy\\data\\"+str(i[:8])+".txt","a+",encoding="utf8") as fp:
fp.write(i[14:])
if i[:8]=="20160216":
with open("e:\\murphy\\data\\"+str(i[:8])+".txt","a+",encoding="utf8") as fp:
fp.write(i[14:])
if i[:8]=="20160217":
with open("e:\\murphy\\data\\"+str(i[:8])+".txt","a+",encoding="utf8") as fp:
fp.write(i[14:])
print(dellWithData())
#第2题
#encodng=utf-8
"""
文件中有两行内容,在中间再加入一行
"""
def addContent():
tempList=[]
fp=open("e:\\murphy\\data\\1.txt","r",encoding="utf8")
tempList=fp.readlines()
print(tempList)
tempList.insert(1,"am\n")
fp.seek(0,0)
with open("e:\\murphy\\data\\1.txt","w",encoding="utf8") as fp:
fp.writelines(tempList)
fp.close()
print(addContent())