Skip to content

Instantly share code, notes, and snippets.

@suziwen
Created April 28, 2013 02:24
Show Gist options
  • Save suziwen/5475598 to your computer and use it in GitHub Desktop.
Save suziwen/5475598 to your computer and use it in GitHub Desktop.
#coding=utf-8
import codecs
import glob
import os
import sys
os.chdir('/home/suziwen/Desktop/temp/usa')
files = sorted(glob.glob('*.txt'),cmp=lambda x,y : cmp(int(x[:-4]),int(y[:-4])))
titleLines = (('<a href="#%d">%s</a>' %(idx , val)) for idx,val in enumerate(files))
titleContent = ('<mpb:pagebreak><center>目录</center><center>%s</center></mpb:pagebreak>' %('<br>'.join(titleLines)))
print titleContent
def transformFileLines(fileName):
file = codecs.open(fileName,'r','gbk')
try :
while True:
t = file.readline()
if not t : break
yield t
finally:
file.close()
def transformFileContents(files):
for idx,fileName in enumerate(files) :
fileLines = (('<p>%s</p>'%(val)) for val in transformFileLines(fileName))
fileBody = '\n'.join(fileLines)
print fileBody
fileContent = '<mbp:pagebreak><h2 id="%d">%s</h2>%s</mbp:pagebreak>' %(idx, fileName,fileBody)
yield fileContent
#print type(transformFileLines)
#print type(transformFileContents)
#x = transformFileLines('01.txt')
#y = transformFileContents(['01.txt','02.txt'])
#print 'gggg'.join(x),'xxxx'.join(y)
f = codecs.open('result.html','a')
print f.encoding
try :
f.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>走进美国</title></head><body>%s' %(titleContent))
tcont = (val for val in transformFileContents(files))
t = '\n'.join(tcont)
(f.write(t))
f.write('</body></html>')
except Exception,e :
print 'error' +e +sys.exc_info()[0]
raise
finally:
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment