千家信息网

Python分割器怎么使用

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要讲解了"Python分割器怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Python分割器怎么使用"吧!# 将txt小说分割转换成
千家信息网最后更新 2026年11月03日Python分割器怎么使用

这篇文章主要讲解了"Python分割器怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Python分割器怎么使用"吧!

  1. # 将txt小说分割转换成多个HTML文件

  2. # @author : GreatGhoul

  3. # @email : greatghoul@gmail.com

  4. # @blog : http://greatghoul.javaeye.com

  5. import re

  6. import os

  7. # regex for the section title

  8. # sec_re = re.compile(r'第.+卷\s+.+\s+第.+章\s+.+')

  9. # txt book's path.

  10. source_path = 'f:\\佣兵天下.txt'

  11. path_pieces = os.path.split(source_path)

  12. novel_title = re.sub(r'(\..*$)|($)', '', path_pieces[1])

  13. target_path = '%s%s_html' % (path_pieces[0], novel_title)

  14. section_re = re.compile(r'^\s*第.+卷\s+.*$')

  15. section_head = '''''

  16. %s

  17. %s

    去页尾
    '''

  18. # escape xml/html

  19. def escape_xml(code):

  20. text = code

  21. text = re.sub(r'<', '<', text)

  22. text = re.sub(r'>', '>', text)

  23. text = re.sub(r'&', '&', text)

  24. text = re.sub(r'\t', '    ', text)

  25. text = re.sub(r'\s', ' ', text)

  26. return text

  27. # entry of the script

  28. def main():

  29. # create the output folder

  30. if not os.path.exists(target_path):

  31. os.mkdir(target_path)

  32. # open the source file

  33. input = open(source_path, 'r')

  34. sec_count = 0

  35. sec_cache = []

  36. idx_cache = []

  37. output = open('%s\\%d.html' % (target_path, sec_count), 'w')

  38. preface_title = '%s 前言' % novel_title

  39. output.writelines([section_head % (preface_title,
    preface_title)])

  40. idx_cache.append('

  41. %s
  42. '

  43. % (sec_count, novel_title))

  44. for line in input:

  45. # is a chapter's title?

  46. if line.strip() == '':

  47. pass

  48. elif re.match(section_re, line):

  49. line = re.sub(r'\s+', ' ', line)

  50. print 'converting %s...' % line

  51. # write the section footer

  52. sec_cache.append('


    ')

  53. if sec_count == 0:

  54. sec_cache.append('目录 | ')

  55. sec_cache.append('下一篇 | '

  56. % (sec_count + 1))

  57. else:

  58. sec_cache.append('上一篇 | '

  59. % (sec_count - 1))

  60. sec_cache.append('目录 | ')

  61. sec_cache.append('下一篇 | '

  62. % (sec_count + 1))

  63. sec_cache.append('回页首

    ')

  64. sec_cache.append('')

  65. output.writelines(sec_cache)

  66. output.flush()

  67. output.close()

  68. sec_cache = []

  69. sec_count += 1

  70. # create a new section

  71. output = open('%s\\%d.html' % (target_path, sec_count), 'w')

  72. output.writelines([section_head % (line, line)])

  73. idx_cache.append('

  74. %s
  75. '

  76. % (sec_count, line))

  77. else:

  78. sec_cache.append('

    %s

    '

  79. % escape_xml(line))

  80. # write rest lines

  81. sec_cache.append('下一篇 | '

  82. % (sec_count - 1))

  83. sec_cache.append('目录 | ')

  84. sec_cache.append('回页首

    ')

  85. output.writelines(sec_cache)

  86. output.flush()

  87. output.close()

  88. sec_cache = []

  89. # write the menu

  90. output = open('%s\\index.html' % (target_path), 'w')

  91. menu_head = '%s 目录' % novel_title

  92. output.writelines([section_head % (menu_head, menu_head),
    '

      '])

    • output.writelines(idx_cache)

    • output.writelines(['

    '])

  93. output.flush()

  94. output.close()

  95. inx_cache = []

  96. print 'completed. %d chapter(s) in total.' % sec_count

  97. if __name__ == '__main__':

  98. main()

感谢各位的阅读,以上就是"Python分割器怎么使用"的内容了,经过本文的学习后,相信大家对Python分割器怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

分割器 目录 学习 内容 前言 多个 天下 宋体 小说 就是 思路 情况 文件 文章 更多 楷体 知识 知识点 篇文章 跟着 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 百单网互联网科技有限公司怎么样 云之行互联网科技 盈实互联网科技 科技企业和互联网企业 互联网科技的发展 互联网 科技创新 上海致骋互联网科技 盛创互联网科技有限公司 哈尔滨秒房互联网科技有限公司 互联网汽车科技小镇 互联网科技公司需要的职位 佳云科技和互联网广告 电子商务与互联网科技的关系 黑云互联网科技有限公司 启辰互联网科技 半导体行业属于互联网高科技类吗 兰石爱特工业互联网科技分公司 恒生科技指数 中概互联网 网互联网络科技 知域互联网科技有限公司官网 科技互联网技术 佰商互联网科技有限公司 海南购全球互联网科技 中国的互联网科技企业的崛起 互联网科技与大数据专业 注册互联网科技公司经营范围大全 电子科技大学互联网创业大赛 广州新华互联网科技学校电竞方面 高科技互联网发展前景如何 世界互联网科技成果奖
0