python文本替换

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:19   1458   0
    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)

转载于:https://www.cnblogs.com/MrRead/p/9101146.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP