实例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/

Python 将文本转换成html的简单示例的更多相关文章

  1. [代码] 类似 YYText 将表情文本转换成表情字符

    一,经历 1> 由于工作需要,得把 UITextView 中的属性文本转换成普通文字,并将处理后的普通文字转换成属性文本. 2> 将属性文本转换成普通文字简单,可以调用属性文本的enume ...

  2. python把汉字转换成拼音实现程序

    python把汉字转换成拼音实现程序 文章一个简洁干的汉字转拼音 程序,复制下载就可以直接使用,有需要的同学可以参考一下下. #coding:utf-8 #基于python2.6 table = 'a ...

  3. Python 将pdf转换成txt(不处理图片)

    上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要.查找了很多资料,在linux下要将d ...

  4. 【Python】使用Python将Shellcode转换成汇编

    1.介绍 需要多少行代码转换hex成反汇编呢? 多亏了Python的Capstone库,做这件事只需要五行. 在二进制分析中,进行Exploit开发或逆向工程时,需要快速将十六进制的Shellcode ...

  5. python将url转换成二维码

    准备环境 python生成二维码,需要依赖包qrcode和PIL(二维码的实质就是一个url.) python3安装PIL:pip install PIL python2安装PIL:pip insta ...

  6. python将字符串转换成字典的几种方法

    当我们遇到类似于{‘a’:1, 'b':2, 'c':3}这种字符串时,想要把它转换成字典进行处理,可以使用以下几种方法: 1. Python自带的eval函数(不安全) dictstr = '{&q ...

  7. python将字符串转换成对应的python数据类型--eval和json.loads(),json.dumps()

    eval()和json.loads() 都可以将字符串转换成对应的python数据类型,举个字典的例子,同样适合其他数据类型元组.列表.集合. In [3]: ss = '{"a" ...

  8. python class对象转换成json/字典

    # -*- encoding: UTF-8 -*- class Student: name = '' age = 0 def __init__(self, name, age): self.name ...

  9. python中数字转换成字符串

    数字转换成字符串: num=123 str='%d' %num str就变成了"123"

随机推荐

  1. ZJOI2008泡泡堂BNB

    1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1305  Solved: 676[Submit][Sta ...

  2. 常用WEB服务器的特点介绍

    经过系统的学习web服务器,现在知道常用的web服务器的优缺点,这对搭建网站架构时选择使用web服务器很有帮助,现在我简单总结一下: 1. Apache:属于重量级web服务器(重量级主要是在软件包的 ...

  3. P2P编程(十)

    此为网络编程的一个系列,后续会把内容补上....

  4. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

  5. Ruby准备工作

    解释性语言,自上而下执行,纯面向对象,跨平台,动态绑定,没有多重继承.NetBeans sun公司开发irb指令可快速实时输入并返回结果 quit 或者exitrdoc hello.rb 生成html ...

  6. css选择器,有箭头与没箭头的区别

    div > span 和 div span 的区别 ,即有箭头和没箭头的区别 div > span span 是 div 的下一层级关系 在这种情况下找得到span元素: <div& ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.7

    The set of all invertible matrices is a dense open subset of the set of all $n\times n$ matrices. Th ...

  8. java语言实现简单接口工具--粗简版

    2016注定是变化的一年,忙碌.网红.项目融资失败,现在有点时间整整帖子~~ 目标: 提高工作效率与质量,能支持平台全量接口回归测试与迭代测试也要满足单一接口联调测试. 使用人员: 测试,开发 工具包 ...

  9. I2c串行总线组成及其工作原理

    采用串行总线技术可以使系统的硬件设计大大简化,系统的体积减小,可靠性提高,同时系统更容易更改和扩充 常用的串行扩展总线有:I2c总线,单总线,SPI总线,以及microwire.Plus等等 I2c总 ...

  10. Gdb 常用命令

    命令名称 含义 示例 b  fun_name 设置断点 b  main b 行号 if  条件 设置带条件断点 如:b 11 if  i==10 n 下一行 n s 跳入函数内部 s  sum fin ...