python文本替换

    file_data = ''
    str1 = '1'
    str2 = '2'
    with open(loginfofile, 'r+') as f:       #打开文件,r+模式,读取
        for line in f:
            if str1 in line:
                line = line.replace(str1, 'n')        #将str替换为n

            if str2 in line:
                line2 = line.replace(str2, 'n')
                file_data += line2
    with open(logproducefile, 'w') as f:            #以写入方式打开文件w
        f.write(file_data)

 

posted @ 2018-05-28 17:20  Mr,Read  阅读(3150)  评论(0编辑  收藏  举报