<listing id="fdftx"></listing>

        <address id="fdftx"></address>
        <address id="fdftx"><address id="fdftx"><nobr id="fdftx"></nobr></address></address>
        <address id="fdftx"><listing id="fdftx"><nobr id="fdftx"></nobr></listing></address>

        <address id="fdftx"><nobr id="fdftx"><meter id="fdftx"></meter></nobr></address><address id="fdftx"><th id="fdftx"><progress id="fdftx"></progress></th></address>

          [使用案例]Python怎么操作批量替換文本內容?

          發布時間:2020-05-28 關注熱度:°C

            現在在Python中使用ip代理的人也比以前要多,ip代理的使用,讓大數據這個詞語在當代互聯網時代變得更加普遍了。如何在Python3 批量替換文本內容?以下跟著IP海來看看教程吧。

          Python批量替換文本內容

            Python3 批量替換文本內容,代碼操作部分:

           

            # coding:utf8

           

            import os;

           

            def reset():

           

            i = 0

           

            path = r"H:sDemoworkdemowesome-android-ui-masterpages\";

           

            filelist = os.listdir(path) # 該文件夾下所有的文件(包括文件夾)

           

            for files in filelist: # 遍歷所有文件

           

            i = i + 1

           

            Olddir = os.path.join(path, files); # 原來的文件路徑

           

            if os.path.isdir(Olddir): # 如果是文件夾則跳過

           

            continue;

           

            filename = os.path.splitext(files)[0]; # 文件名

           

            filetype = os.path.splitext(files)[1]; # 文件擴展名

           

            filePath=path+filename+filetype

           

            alter(filePath, "/art/", "../art/")

           

            def alter(file,old_str,new_str):

           

            """

           

            將替換的字符串寫到一個新的文件中,然后將原文件刪除,新文件改為原來文件的名字

           

            :param file: 文件路徑

           

            :param old_str: 需要替換的字符串

           

            :param new_str: 替換的字符串

           

            :return: None

           

            """

           

            with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:

           

            for line in f1:

           

            if old_str in line:

           

            line = line.replace(old_str, new_str)

           

            f2.write(line)

           

            os.remove(file)

           

            os.rename("%s.bak" % file, file)

           

            if __name__=='__main__':

           

            reset()

           

            以上就是IP海帶來如何在Python實現批量替換文本的操作,希望對各位正在學習Python操作的用戶有幫助。

           

           

           

          版權聲明:本文為IP海(iphai.cn)原創作品,未經許可,禁止轉載!

          Copyright © www.skldkt.com. All Rights Reserved. IP海 版權所有.
          IP海僅提供中國內IP加速服務,無法跨境聯網,用戶應遵守《服務條款》內容,嚴禁用戶使用IP海從事任何違法犯罪行為。
          ICP備案鄂ICP備19030659號-3 公安備案鄂公網安備42100302000141號 計算機軟件著作權證計算機軟件著作權證 ICP/EDI許可證ICP/EDI許可證:鄂B2-20200106

          微信掃一掃咨詢
          国产福利第一页