python即时标记_【Python】读书笔记:Python基础教程-项目1-即时标记
功能:給文本添加HTML標(biāo)記,使得到的文檔能夠在瀏覽器中顯示并能作為一個網(wǎng)頁使用。
要求:
輸入不應(yīng)包含人工代碼或標(biāo)簽;
應(yīng)能處理不同的塊,比如標(biāo)題、段落、列表項(xiàng)、內(nèi)嵌文本(比如被強(qiáng)調(diào)的文本、URL等);
具有可拓展性。
涉及到的知識點(diǎn):(參見《Python基礎(chǔ)教程 第2版》)
要對文件進(jìn)行讀寫(11章),或者至少從標(biāo)準(zhǔn)輸入(sys.stdin)讀取,用print輸出
需要對所有輸入的行進(jìn)行迭代(11章)
需要一些字符串方法(3章)
需要一個或兩個生成器(9章)
可能還需要re模塊(10章)
測試文檔:【text_input.txt】
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:
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).
文本塊生成器:【url.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=[]
標(biāo)記程序:【simple_markup.py】
import sys
import re
from util import *
print '
...'title=True
for block in blocks(sys.stdin):
block=re.sub(r'\*(.+?)\*',r'\1',block)
if title:
print '
'
print block
print ''
else:
print '
'
print block
print '
'print ''
執(zhí)行:
python simple_markup.py text_output.html
輸出:【text_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).
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python即时标记_【Python】读书笔记:Python基础教程-项目1-即时标记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Win10计算器怎么计算反三角函数
- 下一篇: Centos7下怎么安装Zimbra