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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM