自动添加标签

def lines(file):
    for line in file: #如果不是空行返回数据,否则返回空行
        yield line
    yield '\n'


def blocks(file):
    block = []#定义一个空的列表
    for line in lines(file):#循环遍历文件非空行
        if line.strip():#截取一行前后的空白
            block.append(line)#添加到列表
        else: #如果是空行
            yield ''.join(block).strip()#返回
            block = []


import sys, re
from  psutil import *
print('<html><head><title>...</title><body>')#打印Html格式
title = True
#var xxx = blocks(sys.stdin)
# print([i for i in blocks(sys.stdin)])
for block in blocks(sys.stdin):
    block = re.sub(r'\*(.+?)\*', r'<em>\1</em>', block)#把**之间的数据修改成斜体
    if title:#第一行匹配成titile,之后的每行都是段落
         print('<h1>')
         print(block)
         print('</h1>')
         title = False
    else:
         print('<p>')
         print(block)
         print('</p>')
print('</body></html>')

 运行命令:python3 哈哈哈.py < test_input.txt > output.html  	输出到 output.html 



Welcome to World Wide Spam, Inc.

These are the corporate web pages of *World Wide Spam*, Inc. We hope
you find your stay enjoyable, and that you will sample many of our
products.

A short history of the company

World Wide Spam was started in the summer of 2000. The business
concept was to ride the dot-com wave and to make money both through
bulk email and by selling canned meat online.

After receiving several complaints from customers who weren't
satisfied by their bulk email, World Wide Spam altered their profile,
and focused 100 on canned goods. Today, they rank as the world's


13,892nd online supplier of SPAM.
Destinations

From this page you may visit several of our interesting web pages:

 - What is SPAM? (http://wwspam.fu/whatisspam)

 - How do they make it? (http://wwspam.fu/howtomakeit)

 - Why should I eat it? (http://wwspam.fu/whyeatit)

How to get in touch with us

You can get in touch with us in *many* ways: By phone (555-1234), by
email (wwspam@wwspam.fu) or by visiting our customer feedback page
(http://wwspam.fu/feedback).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值