這篇文章主要講解了“Python分割器怎么使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Python分割器怎么使用”吧!

成都創(chuàng)新互聯(lián)公司是專業(yè)的莎車網(wǎng)站建設(shè)公司,莎車接單;提供網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行莎車網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
# 將txt小說分割轉(zhuǎn)換成多個(gè)HTML文件
# @author : GreatGhoul
# @email : greatghoul@gmail.com
# @blog : http://greatghoul.javaeye.com
import re
import os
# regex for the section title
# sec_re = re.compile(r'第.+卷\s+.+\s+第.+章\s+.+')
# txt book's path.
source_path = 'f:\\傭兵天下.txt'
path_pieces = os.path.split(source_path)
novel_title = re.sub(r'(\..*$)|($)', '', path_pieces[1])
target_path = '%s%s_html' % (path_pieces[0], novel_title)
section_re = re.compile(r'^\s*第.+卷\s+.*$')
section_head = '''''
<html>
<head>
<meta http-equiv="Content-Type" content="GBK"/>
<title>%s</title>
</head>
<body style="font-family:楷體,宋體;font-size:16px;
margin:0;padding: 20px; background:#FAFAD2;color:#2B4B86;text
-align:center;"><h3>%s</h3><a href="#bottom">去頁尾</a><hr/>'''
# escape xml/html
def escape_xml(code):
text = code
text = re.sub(r'<', '<', text)
text = re.sub(r'>', '>', text)
text = re.sub(r'&', '&', text)
text = re.sub(r'\t', ' ', text)
text = re.sub(r'\s', ' ', text)
return text
# entry of the script
def main():
# create the output folder
if not os.path.exists(target_path):
os.mkdir(target_path)
# open the source file
input = open(source_path, 'r')
sec_count = 0
sec_cache = []
idx_cache = []
output = open('%s\\%d.html' % (target_path, sec_count), 'w')
preface_title = '%s 前言' % novel_title
output.writelines([section_head % (preface_title,
preface_title)])idx_cache.append('<li><a href="%d.html">%s</a></li>'
% (sec_count, novel_title))
for line in input:
# is a chapter's title?
if line.strip() == '':
pass
elif re.match(section_re, line):
line = re.sub(r'\s+', ' ', line)
print 'converting %s...' % line
# write the section footer
sec_cache.append('<hr/><p>')
if sec_count == 0:
sec_cache.append('<a href="index.html">目錄</a> | ')
sec_cache.append('<a href="%d.html">下一篇</a> | '
% (sec_count + 1))
else:
sec_cache.append('<a href="%d.html">上一篇</a> | '
% (sec_count - 1))
sec_cache.append('<a href="index.html">目錄</a> | ')
sec_cache.append('<a href="%d.html">下一篇</a> | '
% (sec_count + 1))
sec_cache.append('<a name="bottom" href="#">回頁首</a></p>')
sec_cache.append('</body></html>')
output.writelines(sec_cache)
output.flush()
output.close()
sec_cache = []
sec_count += 1
# create a new section
output = open('%s\\%d.html' % (target_path, sec_count), 'w')
output.writelines([section_head % (line, line)])
idx_cache.append('<li><a href="%d.html">%s</a></li>'
% (sec_count, line))
else:
sec_cache.append('<p style="text-align:left;">%s</p>'
% escape_xml(line))
# write rest lines
sec_cache.append('<a href="%d.html">下一篇</a> | '
% (sec_count - 1))
sec_cache.append('<a href="index.html">目錄</a> | ')
sec_cache.append('<a name="bottom" href="
#">回頁首</a></p></body></html>')output.writelines(sec_cache)
output.flush()
output.close()
sec_cache = []
# write the menu
output = open('%s\\index.html' % (target_path), 'w')
menu_head = '%s 目錄' % novel_title
output.writelines([section_head % (menu_head, menu_head),
'<ul style="text-align:left">'])output.writelines(idx_cache)
output.writelines(['</ul><body></html>'])
output.flush()
output.close()
inx_cache = []
print 'completed. %d chapter(s) in total.' % sec_count
if __name__ == '__main__':
main()
感謝各位的閱讀,以上就是“Python分割器怎么使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Python分割器怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)頁題目:Python分割器怎么使用
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article0/gicjio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、自適應(yīng)網(wǎng)站、定制開發(fā)、ChatGPT、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)