RECURRENT DONATION
Donate monthly to support
the NeculaiFantanaru.com project
您可以在此处查看完整代码:HTTPS://帕萨特斌.com/VA56发8PN 安装Python. 假设我们有几个.txt文件。 我们只有一个.html文件 在.html文件的正文中,有本节:<! - 开始文章 - >和<! - 最终项目 - >。 将在此处复制.txt文件中的文本。 您还需要创建一个名为的文件夹:files_html.(此新文件都将保存在此处)。 所有其他文件。 TXT和.html文件在文件夹外部释放:files_html. 您可以根据需要拥有尽可能多的.txt文件。 这样的.txt文件只包含文本,例如: My name probably does not say anything to you, Walter Elias Disney, 您只需要一个.html文件,其中包含以下部分: <p class="obisnuit"><em>Honor your moral and spiritual obligations.em>p> <p class="nint">Bishop knew how to say the most meaningful of things speech.p> 输出:运行代码后,我们将有:
My name probably does not say anything to you, Walter Elias Disney,
Codul:在任何翻译程序中复制并运行以下代码(我在用Pycripter) .不要忘记更改“creare_fisiere_html”行上的路径。 使用hilite.me生成的HTML 进口 你 进口 热 def read_text_from_file.(文件路径):""" 此函数返回文件内容。 file_path:要读取的文件的方式 """ 和 打开(file_path,编码='失败8')作为F: 文本=F.读()返回文本def write_to_file.(text,file_path):""" 此功能在文件中写入文本。 文字:您要写的文字 file_path:要写入的文件的方式 """ 和 打开(文件路径,'WB')作为F: F.写(文本.编码('失败8','忽视'))def copiaza_continut_txt_html.(path_fisier_txt,path_fisier_html):#这些是函数的参数,即当我吸引函数时 #阅读文件中的文本text_txt.=read_text_from_file(cale_fisier_txt) text_html.=read_text_from_file(cale_fisier_html)#我们将文本从文件转换为字符串text_txt.= str(text_txt) text_html.= str(text_html)#这是表达式正则表达式的模式; (。*?)意味着取代标签之间的一切 #修改正则表达式,具体取决于标签作为函数的参数提供的文章_Pattern.=热.编译('<! - 开始文章 - >([s] *?)<! - 最终项目 - > [\ s] *?'的) text_articol.=热.FindAll(Articol_Pattern,Text_html)如果 len。(text_articol)!= 0: text_articol.= str(text_articol [0]) text_txt.= '\ n \ n' +text_txt.+ '\ n \ n'text_html.=text_html..替换(text_articol,text_txt) 文件路径=你.小路.DIRNAME(CALE_FISIER_TXT)+ "\\" + “file_html” + "\\" +你.小路.Splitext(这.小路.BaseName(Cale_fisier_txt))[0]+ '.html'write_to_file(text_html,file_path)打印(“写作成功完成。”)别的:打印(“没有开始/最终项目的HTML文件。”)def creation_fisiere_html.(path_folder_txt,path_fisier_html):""" 该函数由包含TXT文件的文件夹操作,并创建适当的HTML文件 """ 为了F在你.listdir(cale_folder_txt):如果F.以。。结束('文本文件'): path_fisier_txt.=cale_folder_txt.+ "\\" +F。 copiaza_continut_txt_html(path_fisier_txt,path_fisier_html)别的:继续 def 主要的(): creation_fisiere_html(“C:\\folder1“,“C:\\folder1。\\Oana.tuth“)如果__那么__== '__主要的__': 主要的() That's all folks. Latest articles accessed by readers:
Donate via Paypal
RECURRENT DONATIONDonate monthly to support SINGLE DONATIONDonate the desired amount to support Donate by Bank TransferAccount Ron: RO34INGB0000999900448439
Open account at ING Bank
|
||||||||||||
![]() |
||||||||||||