【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:
http://www.c91.com,
http://www.bd.com,
sudley.su@foodly.com.

properties:

|name|height(cm)|weight(kg)

|Sudley|155|200

|sherley|188|20

执行方式:

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

test_input.html在谷歌上的显示效果如下
在这里插入图片描述
源码仓:https://github.com/Sudley/txt2html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值