Python分割器教你给文章做手术

开发 后端
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. <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. <h2>%s</h2><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分割器的相关介绍,希望大家有所收获。

【编辑推荐】

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

2019-03-04 13:59:38

机器人FDA手术

2022-01-10 21:56:21

人工智能AI

2018-06-05 10:24:03

2022-07-05 18:13:27

协和医院腾讯AI

2012-02-08 13:53:56

2020-02-12 10:29:53

Python爬虫公众号

2012-05-14 14:35:41

2020-10-18 17:48:58

机器人人工智能手术

2017-09-05 08:52:37

Git程序员命令

2012-03-08 09:42:16

开源软件Linux

2022-08-26 01:46:33

注册中心NacosDNS

2020-08-12 07:41:39

SQL 优化语句

2020-05-26 10:20:56

Python开发工具

2022-10-08 15:07:06

ChatOps运维

2019-07-15 07:58:10

前端开发技术

2017-11-07 11:40:40

iPhone XHome键苹果

2013-12-16 10:59:52

WiFi上锁WiFi被盗

2012-05-29 14:13:39

Facebook 手机

2022-06-30 16:10:26

Python计时器装饰器

2017-12-15 13:33:04

微信iOSAndroid
点赞
收藏

51CTO技术栈公众号