千家信息网

Python分割器怎么使用

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章主要讲解了"Python分割器怎么使用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Python分割器怎么使用"吧!# 将txt小说分割转换成
千家信息网最后更新 2025年02月02日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分割器怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0