python读写文件

来源:互联网 发布:linux关闭tomcat报错 编辑:程序博客网 时间:2024/06/07 01:09
#coding:utf8def isalary():    with open(u"F:/file1.txt","r") as fp,open(u"information.txt","w") as fi:
            #获取文件的内容 可以直接f=fp.readlines不用下一步            f = fp.read()
            #将读取的数据分割放在列表里            f =f.splitlines()            for one in f:
                #先将每一行去空格                one = one.strip()
                #如果是空行,跳过本次循环                if one == "":                    continue                if one.count(";") != 1:                    continue
                #将字符串分割,获取字段                namepart,salarypart = one.split(";")                salary = int(salarypart.split(":")[1].strip())                name = namepart.split(":")[1].strip()                tax = int(salary*0.1)                incom = int(salary*0.9)                
                #写进文件的时候必须加换行符                co = "name : %-10s,salary : %-10s,tax : %-10d, incom : %-10d\n"%(name,salary,tax,incom)                fi.write(co)isalary()
 
原创粉丝点击