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"
随机推荐
- PopupWindow-弹窗的界面
1 效果图 2 知识点 PopupWindow(View contentView, int width, int height) //创建一个没有获取焦点.长为width.宽为height,内容为 ...
- c语言中 int *p = NULL 和 *p = NULL 有什么区别
1. int *p = NULL; 代表定义一个指向整型变量的指针p,然后p的值设为NULL,也就是设为0:用另一种方式说,就是对一个刚定义的指向整型变量的指针,赋初始值,让其指向0地址. 2. *p ...
- NGINX(四)配置解析
前言 nginx配置解析是在初始化ngx_cycle_t数据结构时,首先解析core模块,然后core模块依次解析自己的子模块. 配置解析过程 nginx调用ngx_conf_parse函数进行配置文 ...
- umask设置导致程序权限不足的问题
这几天邮件告警总是发不了邮件,查看了下zext_msmtp.sh的日志,发现总是提示权限不足…… 于是切换为zabbix的账户,发现在msmtp的目录下连ls都无法执行. 之后发现是umask的问题, ...
- Linux下配置Mysql允许远程访问
操作非常简单,就5步骤,如下: 1.进入 mysql: ? 1 /usr/local/mysql/bin/mysql -u root -p 2.使用 mysql库 : ? 1 use mysql; 3 ...
- mysql日期加减问题
有一个date列,我想修改下,让该列里每个日期都加上7天,怎么写代码? update [表名] set date=date_add(date, interval 7 day); SELECT * f ...
- 动态调用WebService 通用方法Moss 中 传统开发中都可用。
WebService是啥大家都知道了,这里不做过多的解释.通常我们使用WebService的做法基本都是在我们的项目中添加Web引用的方式,首先找到WebService的地址,然后定义命名空间,这样会 ...
- HDU-4704 Sum 大数幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704 题意:求a^n%m的结果,其中n为大数. S(1)+S(2)+...+S(N)等于2^(n-1) ...
- HDU5739-Fantasia(tarjan求割点)
题意:给一个无向图n个点1~n,m条边,sigma(i*zi)%(1e9+7).zi是这个图删掉i点之后的价值.一个图的价值是所有连通子图的价值之和,连通图的价值是每个点的乘积. 题解:讲道理这题不算 ...
- code[vs]3301 Square words
暴力枚举+最长公共子序列 #include <iostream> #include <cstring> using namespace std; int dp[510][510 ...