实例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. Highcharts20151130

    $(function () { $('#container').highcharts({ chart: { type: 'spline' // 图的类型 }, title: { text: null ...

  2. xUTils框架的学习(二)

    这章讲的是框架的DbUtils模块的学习 三 xUtils框架的DButils模块 最开始接触这个框架就是从数据库模块开始的.当时的需求是需要记录用户的登录数据,保存在本地以便进行离线登录.首先想到的 ...

  3. jquery自动生成分页控件 - pagetest.js

    /* pagenum:当前页数 theallnum:总条数 themeiye:每页显示多少条 */ function pagetest(pagenum, theallnum, themeiye) { ...

  4. 一位Erlang程序员的自白

    Erlang不能错过的盛宴 (快步进入Erlang的世界) 作者:成立涛 (litaocheng@gmail.com) 作为程序员,我们曾经闻听很多“业界动态”,“技术革新”,曾经接触很多“高手箴言” ...

  5. Ext入门学习系列(三)复杂自定义窗体

    通过前2节的学习,基本掌握了Ext的语法和运行原理,可以作出一些简单的应用.本节我们一起来完成复杂点的对话框操作,因为在实际项目中经常要用到确认对话框等多种操作,然后根据客户的选择作出不同的响应. 一 ...

  6. POJ 3177 Redundant Paths 边双(重边)缩点

    分析:边双缩点后,消环变树,然后答案就是所有叶子结点(即度为1的点)相连,为(sum+1)/2; 注:此题有坑,踩踩更健康,普通边双缩短默认没有无向图没有重边,但是这道题是有的 我们看,low数组是我 ...

  7. 通过chrome识别手机端app元素--Chrome:inspector

    现实中应该有这样一种情况,就是一个app只支持手机端使用,同时他又是hybrid的,那么其中的webview部分的元素属性如何去获得呢? 使用下面的方法可以解决这个问题: 调试 Android Chr ...

  8. JSP的笔记

    JSP是一种特殊的Servlet,全称是java server page,属于服务器端语言 就JSP和Servle,HTML比较,三者的优缺点简略如下: Servlet: 缺点:不适合作为HTML的响 ...

  9. mysql 处理中文乱码问题

    CREATE TABLE tbl_score( `ID` INT NOT NULL, `score` DEC(,) NOT NULL, `subject` VARCHAR() NOT NULL ); ...

  10. 深度学习-使用cuda加速卷积神经网络-手写数字识别准确率99.7%

    源码和运行结果 cuda:https://github.com/zhxfl/CUDA-CNN C语言版本参考自:http://eric-yuan.me/ 针对著名手写数字识别的库mnist,准确率是9 ...