说明:此项目是“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