Python 将文本转换成html的简单示例
实例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的简单示例的更多相关文章
- [代码] 类似 YYText 将表情文本转换成表情字符
一,经历 1> 由于工作需要,得把 UITextView 中的属性文本转换成普通文字,并将处理后的普通文字转换成属性文本. 2> 将属性文本转换成普通文字简单,可以调用属性文本的enume ...
- python把汉字转换成拼音实现程序
python把汉字转换成拼音实现程序 文章一个简洁干的汉字转拼音 程序,复制下载就可以直接使用,有需要的同学可以参考一下下. #coding:utf-8 #基于python2.6 table = 'a ...
- Python 将pdf转换成txt(不处理图片)
上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要.查找了很多资料,在linux下要将d ...
- 【Python】使用Python将Shellcode转换成汇编
1.介绍 需要多少行代码转换hex成反汇编呢? 多亏了Python的Capstone库,做这件事只需要五行. 在二进制分析中,进行Exploit开发或逆向工程时,需要快速将十六进制的Shellcode ...
- python将url转换成二维码
准备环境 python生成二维码,需要依赖包qrcode和PIL(二维码的实质就是一个url.) python3安装PIL:pip install PIL python2安装PIL:pip insta ...
- python将字符串转换成字典的几种方法
当我们遇到类似于{‘a’:1, 'b':2, 'c':3}这种字符串时,想要把它转换成字典进行处理,可以使用以下几种方法: 1. Python自带的eval函数(不安全) dictstr = '{&q ...
- python将字符串转换成对应的python数据类型--eval和json.loads(),json.dumps()
eval()和json.loads() 都可以将字符串转换成对应的python数据类型,举个字典的例子,同样适合其他数据类型元组.列表.集合. In [3]: ss = '{"a" ...
- python class对象转换成json/字典
# -*- encoding: UTF-8 -*- class Student: name = '' age = 0 def __init__(self, name, age): self.name ...
- python中数字转换成字符串
数字转换成字符串: num=123 str='%d' %num str就变成了"123"
随机推荐
- HAOI2011 problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1047 Solved: 434[Submit][ ...
- 信息学院第九届ACM程序设计竞赛题解
A: 信号与系统 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 238 Accepted: 44 Page View: 69 Des ...
- c语言编译预处理和条件编译执行过程的理解
在C语言的程序中可包括各种以符号#开头的编译指令,这些指令称为预处理命令.预处理命令属于C语言编译器,而不是C语言的组成部分.通过预处理命令可扩展C语言程序设计的环境. 一.预处理的工作方式 1.1. ...
- 也用 Log4Net 之走进Log4Net (四)
转载地址:http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 我是转的别人的内容,我觉得他写的非常好,所以我把其中三分之二转了过来 ...
- 在MFC[转载]在MFC状态栏显示时间 状态栏显示时间
原文:在MFC状态栏显示时间,谢小哈皮儿 c/c++ vc 在mfc状态栏显示时间,在VC的控件中有个Status bar可以在窗体状态栏中添加日期和时间.其实通过简单的代码,你就能创建一个有时钟显示 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.7
For every matrix $A$, the matrix $$\bex \sex{\ba{cc} I&A\\ 0&I \ea} \eex$$ is invertible and ...
- java 多线程下载
import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { /** * 把一 ...
- Jmeter初步使用--Jmeter安装与使用
由于jmeter是开源的性能测试工具,但是使用的人较少,网络上相关资料不全面,需要自己去揣摩,因此,小编就把自己在用Jmeter时的一些小结总结出来.而loadrunner工具是商业软件,网上通常都是 ...
- android 实现模拟按键
android 实现模拟按键方法一 通过Runtime实现,代码如下: try { String keyCommand = "input keyevent " + KeyEvent ...
- 【译】 AWK教程指南 7AWK应用实例
本节将示范一个统计上班到达时间及迟到次数的程序. 这程序每日被执行时将读入两个数据文件: * 员工当日到班时间的数据文件 ( 如下列的 arr.dat ) * 存放员工当月迟到累计次数的文件 当程序执 ...