2019-05-18:继续文件的学习


#第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())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值