python讲TXT批处理成HTML文件
将txt文件分块的模块util.py:deflines(file):forlineinfile:yieldlineyield'\n'defblocks(file):blo...
将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>' 展开
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>' 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询