ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

python:复制HTML文件正文中的文本文件的内容

On December 23, 2021, in Leadership Quantum-XX, by Neculai Fantanaru

您可以在此处查看完整代码: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, 
because I just came back from New York and got only 40 dollars in my pocket.
Increasingly, I need spiritual points to help me not get swallowed by the
most painful miscarriage, the saddest abandon in a labyrinth of vanity
where the only marks are self-forgetting and the others, followed in the
end of supreme sacrifice.

您只需要一个.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, 
because I just came back from New York and got only 40 dollars in my pocket.
Increasingly, I need spiritual points to help me not get swallowed by the
most painful miscarriage, the saddest abandon in a labyrinth of vanity
where the only marks are self-forgetting and the others, followed in the
end of supreme sacrifice.

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.

另外,看到这个版本2.要么 版本3.要么版本4.要么版本5.要么版本6.要么版本7.


Latest articles accessed by readers:

  1. An Eye To See And A Mind To Understand
  2. Turn Towards Me With An Eye Full Of Your Own Gaze
  3. The Snapshot Of Magic In God's Universe
  4. Rhythm Of My Heart

Donate via Paypal

Alternate Text

RECURRENT DONATION

Donate monthly to support
the NeculaiFantanaru.com project

SINGLE DONATION

Donate the desired amount to support
the NeculaiFantanaru.com project

Donate by Bank Transfer

Account Ron: RO34INGB0000999900448439

Open account at ING Bank

Join The Neculai Fantanaru Community



* Note: If you want to read all my articles in real time, please check the romanian version !

decoration
About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
© Neculai Fântânaru - All rights reserved