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.    
  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分割器的相关介绍,希望大家有所收获。

【编辑推荐】

  1. Python数据编组对文字串的读写
  2. Python 拼写检查如何更简单的使用
  3. Python函数变量在应用中的“窍门”
  4. 在Python函数变量中如何使用global语句简介
  5. Python编程语言维和受到众人的追捧

当前名称:Python分割器教你给文章做手术
网址分享:http://www.hantingmc.com/qtweb/news40/412440.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联