Python分割器怎么使用

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

成都创新互联公司是专业的莎车网站建设公司,莎车接单;提供网站建设、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行莎车网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

  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.    

  17.    

  18.    

  19. %s   

  20.    

  21. margin:0;   

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

  23. %s

    去页尾
    '''   

  24. # escape xml/html   

  25. def escape_xml(code):   

  26. text = code   

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

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

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

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

  31. text = re.sub(r'\s', ' ', 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('

  47. %s
  48. '   

  49. % (sec_count, novel_title))   

  50. for line in input:   

  51. # is a chapter's title?   

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

  53. pass   

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

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

  56. print 'converting %s...' % line   

  57. # write the section footer   

  58. sec_cache.append('


    ')   

  59. if sec_count == 0:   

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

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

  62. % (sec_count + 1))   

  63. else:   

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

  65. % (sec_count - 1))   

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

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

  68. % (sec_count + 1))   

  69. sec_cache.append('回页首

    ')   

  70. sec_cache.append('')   

  71. output.writelines(sec_cache)   

  72. output.flush()   

  73. output.close()   

  74. sec_cache = []   

  75. sec_count += 1   

  76. # create a new section   

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

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

  79. idx_cache.append('

  80. %s
  81. '   

  82. % (sec_count, line))   

  83. else:   

  84. sec_cache.append('%s

    '   

  85. % escape_xml(line))   

  86. # write rest lines   

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

  88. % (sec_count - 1))   

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

  90. sec_cache.append('#">回页首

    ')   

  91. output.writelines(sec_cache)   

  92. output.flush()   

  93. output.close()   

  94. sec_cache = []   

  95. # write the menu   

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

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

  98. output.writelines([section_head % (menu_head, menu_head), 
    ''])   

  99. output.writelines(idx_cache)   

  100. output.writelines([''])   

  101. output.flush()   

  102. output.close()   

  103. inx_cache = []   

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

  105. if __name__ == '__main__':   

  106. main()  

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


当前名称:Python分割器怎么使用
文章转载:http://csdahua.cn/article/gicjio.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流