Python项目学习一:项目一:自动添加标签-初次实现

说明:此项目是“Python基础教程”里面的。另外,我学习用的开发工具是PyCharm,包管理器用的Ananconda Nagigator。详细内容说明,参考我的Python学习心得博客。

书中原始内容,请自行去看电子书、纸质书,或者留言,我给你大家。 

开篇直接先上代码:

初次实现(原型)

准备的文本内容:

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:

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

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

7Why 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	20
(http://wwspam.fu/feedback).

代码实现:

"""
名称定义为util.py
"""
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)

        elif block:
            yield ''.join(block).strip()
            block = []

 

import sys, re
from util import *
"""
脚本名称定义为simple_markup.py
和原书中不同,在读取文件时,我用的open函数,其中把test_input.txt文件放到和脚本同一个目录;否则需要加绝对路径。
"""

print('<html><head><title>...</title><body>')

title = True
for block in blocks(open("test_input.txt","r+")):
    block = re.sub(r'\*(.+?)\*', r'<em>\1</em>', block)
    if title:
        print('<h1>')
        print(block)
        print('</h1>')
        title = False
    else:
        print('<p>')
        print(block)
        print('</p>')

print('</body></html>')

 注意事项:

运行:

<html><head><title>...</title><body>
<h1>
Welcome to World Wide Spam, Inc.
</h1>
<p>
These are the corporate web pages of <em>World Wide Spam</em>, Inc. We hope you find your stay enjoyable, and that you will sample many of our products.
</p>
<p>
A short history of the company
</p>
<p>
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.
</p>
<p>
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
</p>
<p>
13,892nd online supplier of SPAM. Destinations
</p>
<p>
From this page you may visit several of our interesting web pages:
</p>
<p>
5What is SPAM? (http://wwspam.fu/whatisspam)
</p>
<p>
6How do they make it? (http://wwspam.fu/howtomakeit)
</p>
<p>
7Why should I eat it? (http://wwspam.fu/whyeatit) How to get in touch with us
</p>
<p>
You can get in touch with us in <em>many</em> ways: By phone (555-1234), by
</p>
<p>
email (wwspam@wwspam.fu) or by visiting our customer feedback page	20
</p>
<p>
(http://wwspam.fu/feedback).
</p>
</body></html>

 以上内容,保存到output.html文件中,然后在浏览器打开,即可开到效果:

至此,项目的简单实现,已完成。

再次实现代码:

https://blog.csdn.net/weixin_42163563/article/details/103254693

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖哥真不错

您的鼓励,将是我最大的坚持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值