700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【Python】将txt文件转换为html

【Python】将txt文件转换为html

时间:2022-08-19 15:26:00

相关推荐

【Python】将txt文件转换为html

分析txt文件内容,并按照以下规则及过滤器对文档添加对应的html标签

规则:

标题是只包含一行的文本块,长度70个字符以内。以冒号(:)结束的文本块不属于标题。题目是文档的第一个文本块,且满足标题的规则。列表项是以连字符(-)打头的文本块。列表以紧跟在非列表项文本块后面的列表项开头,以后面紧跟着非列表项文本块的列表项结束。表格行是以竖线符号(|)打头的文本块,行内的列也是使用竖线分隔。表格以紧跟在非表格行文本块后面的表格行开头,以后面紧跟着非表格行文本块的表格行结束。

使用正则表达式对文本块内容进行过滤,分别对尖括号内的内容,星号内的内容和网站及邮箱进行过滤

以下四个正则表达式对应过滤条件:

r'\<(.+?)\>'r'\*(.+?)\*'r'(http(s){0,1}://[\.a-zA-z0-9/]+)'r'([\.a-zA-z0-9]+@[\.a-zA-z0-9]+[a-zA-z]+)'

各文本块之间用一个或多个空行间隔开,示例文档

Welcome to Foodly ,Inc.There π are the corporate web pages of *Foodly*.We hope you find your stay enjoyable,and that you will sample many of our product.A short history of th company......*Parsing HTML*Use the BeautifulSoup class to parse an HTML document. Here are some of the things that BeautifulSoup knows:- Some tags can be nested (<BLOCKQUOTE>) and some can't (<P>).- Table and list tags have a natural nesting order. For instance, <TD> tags go inside <TR> tags, not the other way around.- The contents of a <SCRIPT> tag should not be parsed as HTML.- A <META> tag may specify an encoding for the document.Connect:,,sudley.su@.properties:|name|height(cm)|weight(kg)|Sudley|155|200|sherley|188|20

执行方式:

python3 markup.py < test_input.txt > test_input.html

test_input.html在谷歌上的显示效果如下

源码仓:/Sudley/txt2html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。