温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python分割器怎么使用

发布时间:2021-12-01 14:07:48 来源:亿速云 阅读:131 作者:iii 栏目:编程语言

这篇文章主要讲解了“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. <html>   

  17. <head>   

  18. <meta http-equiv="Content-Type" content="GBK"/>   

  19. <title>%s</title>   

  20. </head>   

  21. <body style="font-family:楷体,宋体;font-size:16px; 
    margin:0;   

  22. padding: 20px; background:#FAFAD2;color:#2B4B86;text
    -align:center;">   

  23. <h3>%s</h3><a href="#bottom">去页尾</a><hr/>'''   

  24. # escape xml/html   

  25. def escape_xml(code):   

  26. text = code   

  27. text = re.sub(r'<', '&lt;', text)   

  28. text = re.sub(r'>', '&gt;', text)   

  29. text = re.sub(r'&', '&amp;', text)   

  30. text = re.sub(r'\t', '&nbsp;&nbsp;&nbsp;&nbsp;', text)   

  31. text = re.sub(r'\s', '&nbsp;', text)   

  32. return text   

  33. # entry of the script   

  34. def main():   

  35. # create the output folder   

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

  37. os.mkdir(target_path)   

  38. # open the source file   

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

  40. sec_count = 0   

  41. sec_cache = []   

  42. idx_cache = []   

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

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

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

  46. idx_cache.append('<li><a href="%d.html">%s</a></li>'   

  47. % (sec_count, novel_title))   

  48. for line in input:   

  49. # is a chapter's title?   

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

  51. pass   

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

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

  54. print 'converting %s...' % line   

  55. # write the section footer   

  56. sec_cache.append('<hr/><p>')   

  57. if sec_count == 0:   

  58. sec_cache.append('<a href="index.html">目录</a>&nbsp;|&nbsp;')   

  59. sec_cache.append('<a href="%d.html">下一篇</a>&nbsp;|&nbsp;'   

  60. % (sec_count + 1))   

  61. else:   

  62. sec_cache.append('<a href="%d.html">上一篇</a>&nbsp;|&nbsp;'   

  63. % (sec_count - 1))   

  64. sec_cache.append('<a href="index.html">目录</a>&nbsp;|&nbsp;')   

  65. sec_cache.append('<a href="%d.html">下一篇</a>&nbsp;|&nbsp;'   

  66. % (sec_count + 1))   

  67. sec_cache.append('<a name="bottom" href="#">回页首</a></p>')   

  68. sec_cache.append('</body></html>')   

  69. output.writelines(sec_cache)   

  70. output.flush()   

  71. output.close()   

  72. sec_cache = []   

  73. sec_count += 1   

  74. # create a new section   

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

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

  77. idx_cache.append('<li><a href="%d.html">%s</a></li>'   

  78. % (sec_count, line))   

  79. else:   

  80. sec_cache.append('<p style="text-align:left;">%s</p>'   

  81. % escape_xml(line))   

  82. # write rest lines   

  83. sec_cache.append('<a href="%d.html">下一篇</a>&nbsp;|&nbsp;'   

  84. % (sec_count - 1))   

  85. sec_cache.append('<a href="index.html">目录</a>&nbsp;|&nbsp;')   

  86. sec_cache.append('<a name="bottom" href="
    #">回页首</a></p></body></html>')   

  87. output.writelines(sec_cache)   

  88. output.flush()   

  89. output.close()   

  90. sec_cache = []   

  91. # write the menu   

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

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

  94. output.writelines([section_head % (menu_head, menu_head), 
    '<ul style="text-align:left">'])   

  95. output.writelines(idx_cache)   

  96. output.writelines(['</ul><body></html>'])   

  97. output.flush()   

  98. output.close()   

  99. inx_cache = []   

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

  101. if __name__ == '__main__':   

  102. main()  

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI