繁体   English   中英

使用python正则表达式在两个文件之间进行替换

[英]Using python regular expressions to sub between two files

基本上,我试图从文本文件中读取文本,使用正则表达式将其细分为其他内容,然后将其写入html文件。

这是我所拥有的片段:

from re import sub

def markup():
    ##sub code here
    sub('[a-z]+', 'test', file_contents)

问题似乎出在那条子线上。 下面的代码(同一功能的一部分)需要制作带有字幕文本的html文件。

    ## write the HTML file
    opfile = open(output_file, 'w') 
    opfile.write('<html>\n')    
    opfile.write('<head>\n') 
    opfile.write('<title>') 
    opfile.write(file_title) 
    opfile.write('</title>\n') 
    opfile.write('</head>\n') 
    opfile.write('<body>\n')
    opfile.write(file_contents)
    opfile.write('</body>\n')
    opfile.write('</html>')
    opfile.close()

设计此功能的目的是使我可以从多个文件中提取文本。 调用标记函数后,我可以复制file_contents之后的所有内容,但括号中的内容除外,我将其替换为其他文件的名称。

def content_func():
    global file_contents
    global file_title
    global output_file
    file_contents = open('example.txt', 'U').read()
    file_title = ('example')
    output_file = ('example.html')
    markup()

content_func()

Example.txt只是一个文本文件,其中包含文本“快速的棕色狐狸跳过懒狗”。 我希望实现的是在文本中搜索特定的标记语言,并将其替换为HTML标记,但是我在这里对其进行了简化,以帮助我尝试解决它。

从理论上讲,运行此代码应创建一个名为example.html的html文件,其标题和文本为“ test”,但是事实并非如此。 我对正则表达式不熟悉,它们使我发疯。 谁能建议我对正则表达式“ sub”应该怎么做?

编辑:该代码不会产生任何错误,但输出的HTML文件缺少任何替换的文本。 因此,该子项正在搜索外部文本文件,但未将其放入输出HTML文件中。

您永远不会保存sub()的结果。 更换

sub('[a-z]+', 'test', file_contents)

有了这个

file_contents = sub('[a-z]+', 'test', file_contents)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM