2020/10/23 : Python 파일 작업

PYTHON 파일 작업

데이터를 처리 할 때 텍스트 파일을 편집해야하는 경우가 많고 수동 편집은 지루하고 시간이 많이 걸립니다 .Python을 사용하여 일괄 처리를 수행하는 것이 훨씬 쉽습니다.

암호

import re
import os

def alter(file,file1,old_str,new_str):
    file_data =""
    with open(file,"r") as f:
        for line in f:
            if old_str in line:
                line = line.replace(old_str,new_str)
            file_data +=line
    with open(file1,"w") as f:
        f.write(file_data)

alter("first.info","second.info","str1","str2")

코드 분석

어느 first.info(file); 원래 수정해야 파일을 참조
second.info저장된 파일을 말한다
str1; 필요가 수정 될 수있는 파일에 표시가 된 문자열을 말한다
str2교체에 사용되는 문자열을 의미한다;

추천

출처blog.csdn.net/weixin_43624728/article/details/109244057