买了一本Python入门,奈何看不下去,只能是先看后面的项目,看到那里不懂的时候在回去学习。

项目名字:即时标记

大致的意思就是把一个纯文本文件标记成自己想要的格式文件。

首先就是待处理文本,我找不到电子版的,就自己手写了一份

Welcome to World Wide Spam, Inc

These are the corporate web pages of *World Wide Spam*, Inc. We hopeyou find your 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 customer who weren't satisfied bu their bulk email .World Wide Spam altered their profile. and foused 100% on canned goods. Today they rank as the world's 13.892nd online suppler of SPAM

Destinations

From this page you may visit several of our interesting web pages:
-What is SPAM?(WWW.baidu.com)
-How do they make it?(WWW.baidu.com)
-Why should i eat is?(WWWW.baidu.com) How to get in touch with us
You can get in touch with us in *many* ways: By phone(123456789). by email(dream_dog@163.com) or by visiting our customer feedback page(wwww.baidu.com)

第一步,就是那文件切分成段落。

找出块的一个简单方法就是搜集遇到的所有行,知道遇到一个空行,然后返回已经搜集的行。那些返回的行就是一个块,之后在开始收集,不需要手机空行,也不要返回空块,同时要确保文件的最后一行是空行,否则程序就不知道什么时候结束

编写一个文件快生成器

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 = []

代码中,lines生成器只是在文件尾追加一行空行,blocks生成器实现了前面说的方法。

添加一些标记

import sys. 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(block)
print('</h1>')
title = false
else:
print('<p>')
print(block)
print('</p>')
print('</body></html>')

这里抱错了,显示我的sys不是一个包

最新文章

  1. [moka同学笔记]PHPexcel之excel导出和导入
  2. AJAX跨域访问(从Tomcat8到Apache/Nginx)
  3. 一条诡异的insert语句
  4. 为VS集成IL环境
  5. [经验分享] 最近调试FT232H遇到的坑
  6. Java JSON、XML文件/字符串与Bean对象互转解析
  7. angularjs取Sevice和directive的引用
  8. 关于C#资源文件操作的总结
  9. Jersey(1.19.1) - Life-cycle of Root Resource Classes
  10. struct timespec 和 struct timeval
  11. MYSQL连接数据库
  12. PHP全局变量
  13. Servlet 的基本架构
  14. JAVA 程序发布引发性能抖动
  15. laytpl--前端数据绑定
  16. POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
  17. 洛谷 [P251] 餐巾计划问题
  18. python实现列表去重的方法
  19. Linux 文件目录管理命令
  20. Hdoj 2717.Catch That Cow 题解

热门文章

  1. 在CentOs6.5下安装Python2.7.6和Scrapy
  2. LeetCode13.罗马数字转整数 JavaScript
  3. Vue node.js商城-购物车模块
  4. JVM垃圾回收补充知识点
  5. 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
  6. Kong Api 初体验
  7. java中的==、equals()源码分析
  8. c#解析分析SQL语句
  9. LeetCode 中级 - 翻转矩阵后的得分(861)
  10. JQuery实现注册表单验证