当前位置: 首页 > news >正文

【Python】读书笔记:Python基础教程-项目1-即时标记

功能:给文本添加HTML标记,使得到的文档能够在浏览器中显示并能作为一个网页使用。

要求:

  1. 输入不应包含人工代码或标签;
  2. 应能处理不同的块,比如标题、段落、列表项、内嵌文本(比如被强调的文本、URL等);
  3. 具有可拓展性。

涉及到的知识点:(参见《Python基础教程 第2版》)

  1. 要对文件进行读写(11章),或者至少从标准输入(sys.stdin)读取,用print输出
  2. 需要对所有输入的行进行迭代(11章)
  3. 需要一些字符串方法(3章)
  4. 需要一个或两个生成器(9章)
  5. 可能还需要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:

  • 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).

文本块生成器:【url.py】

def lines(file):for line in file:yield lineyield '\n'
def blocks(file):block=[]for line in lines(file):if line.strip():block.append(line)elif block:yield ''.join(block).strip()block=[]

标记程序:【simple_markup.py】

import sys
import re
from util import *
print '<html><head><title>...</title><body>'
title=True
for block in blocks(sys.stdin):block=re.sub(r'\*(.+?)\*',r'<em>\1</em>',block)if title:print '<h1>'print blockprint '</h1>'else:print '<p>'print blockprint '</p>'
print '</body></html>'

执行:

python simple_markup.py <text_input.txt> text_output.html

输出:【text_output.html】

<html><head><title>...</title><body>
<h1>
Welcome to World Wide Spam, Inc.
</h1>
<h1>
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.
</h1>
<h1>
A short history of the company
</h1>
<h1>
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.
</h1>
<h1>
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.
</h1>
<h1>
Destinations
</h1>
<h1>
From this page you may visit several of our interesting web pages:
</h1>
<h1>
- What is SPAM? (http://wwspam.fu/whatisspam)
</h1>
<h1>
- How do they make it? (http://wwspam.fu/howtomakeit)
</h1>
<h1>
- Why should I eat it? (http://wwspam.fu/whyeatit)
</h1>
<h1>
How to get in touch with us
</h1>
<h1>
You can get in touch with us in <em>many</em> ways: By phone (555-1234), by
email (wwspam@wwspam.fu) or by visiting our customer feedback page
(http://wwspam.fu/feedback).
</h1>
</body></html>

http://www.taodudu.cc/news/show-4565071.html

相关文章:

  • 【physx/wasm】在physx中添加自定义接口并重新编译wasm
  • excel---常用操作
  • Lora训练Windows[笔记]
  • linux基础指令讲解(ls、pwd、cd、touch、mkdir)
  • InnoDB 事务处理机制
  • 启明云端ESP32 C3 模组WT32C3通过 MQTT 连接 AWS
  • java核心技术读书笔记—继承
  • “Let’s Eat Grandma”:标点符号(句法树)增强语义表达,用于情感分析
  • Java编程思想读书笔记——第十章:内部类
  • Python基础教程(第2版)读书笔记
  • 基于安卓的公司员工考勤系统的设计与实现
  • 职工考勤管理系统
  • 员工考勤管理系统html,一种员工考勤管理系统的制作方法
  • 内存的类型
  • Linux命令_Note1
  • 计算机体系结构——内存
  • 《Unity Shader入门精要》笔记02 第1章+第2章
  • 30天自制OS学习笔记 (四)C语言与画面显示的练习
  • 自制操作系统日志——第四天
  • Linux Graphics 周刊(第 5 期)
  • unity3D 移动平台性能优化
  • 计算机外围设备
  • 轻量级模型设计/部署
  • 30天自制操作系统学习-第8天
  • 【操作系统】30天自制操作系统--(9)叠加处理
  • C语言VRAM字符串平滑移动,航空数字化仪表中动画显示技术的应用
  • 第4天:C语言与画面显示的练习
  • 显卡 内存分配 linux,【原创】Linux环境下的图形系统和AMD R600显卡编程(4)——AMD显卡显存管理机制...
  • 用C语言开发NES游戏(CC65)03、VRAM缓冲区
  • dm8148 开发只boot启动参数vram=128简介
  • 虚拟机服务器CPU授权,vSphere5全新的许可授权方式——CPU许可+vRAM授权
  • 8位色320*200分辨率下的屏幕坐标与VRAM地址计算
  • VRAM
  • CCE to REG bundle
  • 华为云云容器引擎CCE踩坑记
  • 华为云CCE集群节点磁盘告警处理