实例txt文件test_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 if the company

World Wide Spam was started in the summer of 2000.The business
concept was to ride the dot-com wave ande 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 intersting 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/whyeatif)

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

将txt文件分块的模块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=[]

简单的转换模块simple_markup.py:

import sys,re
from util import *

print '<html><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>'

转换代码:python simple_markup.py<test_input.txt> test_output.html

代码执行过后当前目录会产生一个html文件test_output.html,放入浏览器运行可观察效果。

关于代码的注释部分可以参看http://1.imablog.sinaapp.com/exam-translate-txt-html/

最新文章

  1. 环境变量NLS_LANG
  2. cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析
  3. cf251.2.C (构造题的技巧)
  4. 基于MATLAB的adaboost级联形式的人脸检测实现
  5. PHP入门 - - 06--&gt;HTML的表格标签
  6. PHP程序z中xdebug工具简要使用方法
  7. Functional programming
  8. SharePoint开发 - 自定义导航菜单(一)菜单声明与配置
  9. 如何用java实现使用电子邮件控制你的电脑
  10. jquery学习(2)toggle
  11. jsxtransformer.js 和browser.js有什么关系?
  12. [android更新类的内容开发APP]四、项目布局的基本功能(继续)
  13. 总结C++中取成员函数地址的几种方法
  14. oracle数据库的一次异常起停处理。
  15. SpringBoot项目在新电脑上的配置运行,包括JDK+MAVEN+Git+SpringBoot配置等
  16. Windows Server 2008 R2 64位操作系统安装Oracle 11g 64位数据库,在客户终端上安装Oracle 11g 32位,才能安装P/L Sql Developer并配置
  17. springboot测试
  18. Spring Boot REST(二)源码分析
  19. JavaScript -- 数组Array
  20. Ubuntu18.04编译S3的Linux SDK(Zeta)

热门文章

  1. Linux power supply class hacking
  2. Button 自定义(一)-shape
  3. 430flash的操作
  4. MFC消息映射机制
  5. NAND FLASH 原理
  6. 609B Load Balancing
  7. Web技术导论复习大纲
  8. Tomcat普通用户启动注意事项
  9. 推荐vpn的文章
  10. elecworks 图框管理器