[python⼩⼯具]⼩说分割器写本⽂的思路很简单:
⾃⼰是⼀个⼩说迷,有时候就想着能不能把⼀个整本的⼩说给分割成⼀个个单章存在的⽂本⽂件
之前也在⽹上到过别⼈写的软件,然后最近突然想到,能否⽤python实现⼀下
其实有了这个⽬标,实现起来很简单:
最核⼼的就是匹配关键字符串
整体代码如下
# -*- coding: utf-8 -*-
# @Date    : 2018-11-02 17:38:53
# @Author  : Jimy_Fengqi (jmps515@163)
# @Link    : blog.csdn/qiqiyingse
# @Version  : V1.0
'''
将txt⼩说分割转换成单个章节⽂件
⽂件名字以章节命名
本⽂运⾏在python3上⾯,
处理⼩说的时候,需要将⼩说的格式以utf-8保存
(处理以ANSI编码格式的txt⽂本会出现错误)
'''
import re
import os
import sys
# txt book's path.
novel_name='' #⼩说名字
source_path = os.getcwd()+'\\'+novel_name
path_pieces = os.path.split(source_path)writelines使用方法python
novel_title = re.sub(r'(\..*$)|($)', '', path_pieces[1])
target_path = '%s\\%s' % (path_pieces[0], novel_title)#⼩说分章⽬录
section_re = repile(r'^\s*第.+章\s+.*$')
# entry of the script
def main():
# create the output folder
if not ists(target_path):
os.mkdir(target_path)
# open the source file
input = open(source_path, 'r',encoding='utf-8')
sec_count = 0
sec_cache = []
title_cache=[]
output = open('%s\\前⾔.txt' % (target_path), 'w',encoding='utf-8')
preface_title = '%s 前⾔' % novel_title
output.writelines(preface_title)
for line in input:
# is a chapter's title?
# is a chapter's title?
#if line.strip() == '':  #去掉空⾏
#    pass
if re.match(section_re, line):
line = re.sub(r'\s+', ' ', line)
print ('converting %s...' % line)
output.writelines(sec_cache)
output.flush()
output.close()
sec_cache = []
sec_count += 1
#chapter_name=re.sub('(~|!+|\(+|\)+|~+|\(+|\)+|(+|!+)','_',line)
chapter_name=re.sub('(~+|\*+|\,+|\?+|\,+|\?+)','_',line)#章节名字当⽂件名字时,不能有特殊符号
# create a new section
output = open('%s\\%s.txt' % (target_path, chapter_name), 'w',encoding='utf-8')
output.writelines(line)
title_cache.append(line+'\n')
else:
sec_cache.append(line)
output.writelines(sec_cache)
output.flush()
output.close()
sec_cache = []
# write the menu
output = open('%s\\⽬录.txt' % (target_path), 'w',encoding='utf-8')
menu_head = '%s ⽬录' % novel_title
output.writelines(menu_head)
output.writelines(title_cache)
output.flush()
output.close()
inx_cache = []
print ('completed. %d chapter(s) in total.' % sec_count)
if __name__ == '__main__':
main()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。