自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python分割器教你給文章做手術(shù)

開發(fā) 后端
Python分割器能夠幫助我們把長文章進(jìn)行分割。但是要如何才能熟練使用呢?下面我們就來詳細(xì)的學(xué)習(xí)相關(guān)的操作過程。

Python分割器在我們進(jìn)行文章分割的時候會經(jīng)常用到。當(dāng)然一篇相當(dāng)長的文章會讓你有些頭疼。看完下面的代碼希望大家能夠熟練的使用Python分割器進(jìn)行文章分割。

  1. # 將txt小說分割轉(zhuǎn)換成多個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分割器的相關(guān)介紹,希望大家有所收獲。

【編輯推薦】

  1. Python數(shù)據(jù)編組對文字串的讀寫
  2. Python 拼寫檢查如何更簡單的使用
  3. Python函數(shù)變量在應(yīng)用中的“竅門”
  4. 在Python函數(shù)變量中如何使用global語句簡介
  5. Python編程語言維和受到眾人的追捧
責(zé)任編輯:張浩 來源: CSDN
相關(guān)推薦

2019-03-04 13:59:38

機(jī)器人FDA手術(shù)

2022-01-10 21:56:21

人工智能AI

2018-06-05 10:24:03

2012-02-08 13:53:56

2022-07-05 18:13:27

協(xié)和醫(yī)院騰訊AI

2020-02-12 10:29:53

Python爬蟲公眾號

2017-09-05 08:52:37

Git程序員命令

2020-10-18 17:48:58

機(jī)器人人工智能手術(shù)

2012-05-14 14:35:41

2022-10-08 15:07:06

ChatOps運(yùn)維

2012-03-08 09:42:16

開源軟件Linux

2020-08-12 07:41:39

SQL 優(yōu)化語句

2022-08-26 01:46:33

注冊中心NacosDNS

2019-07-15 07:58:10

前端開發(fā)技術(shù)

2020-05-26 10:20:56

Python開發(fā)工具

2017-12-15 13:33:04

微信iOSAndroid

2017-11-07 11:40:40

iPhone XHome鍵蘋果

2013-12-16 10:59:52

WiFi上鎖WiFi被盜

2021-03-08 09:15:46

日志Filebeat運(yùn)維

2012-05-29 14:13:39

Facebook 手機(jī)
點贊
收藏

51CTO技術(shù)棧公眾號