Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python Splitter

Shulou Source: shulou.com Published: 2022-06-02 09:05:38 09月22日 Update

This article mainly explains the "Python splitter how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python splitter how to use" it!

# split the txt novel into multiple HTML files

# @ author: GreatGhoul

# @ email: greatghoul@gmail.com

# @ blog: http://greatghoul.javaeye.com

Import re

Import os

# regex for the section title

# sec_re = re.compile (r'. + Volume\ sroom.+\ s+ Chapter.

# txt book's path.

Source_path ='f:\\ mercenary .txt'

Path_pieces = os.path.split (source_path)

Novel_title = re.sub (r'(\.. * $) | ($)','', path_pieces [1])

Target_path ='% s% sroomhtml'% (path_pieces [0], novel_title)

Section_re = re.compile (r'^\ s* volume. + volume\ sroom.resume')

Section_head ='

% s

% s go to footer''

# escape xml/html

Def escape_xml (code):

Text = code

Text = re.sub (rushing,'>', text)

Text = re.sub (ringing girls,'&', text)

Text = re.sub (r'\ tasking,'', text)

Text = re.sub (r'\ s,'', text)

Return text

# entry of the script

Def main ():

# create the output folder

If not os.path.exists (target_path):

Os.mkdir (target_path)

# open the source file

Input = open (source_path,'r')

Sec_count = 0

Sec_cache = []

Idx_cache = []

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

Preface_title ='% s foreword'% novel_title

Output.writelines ([section_head% (preface_title)

Preface_title)])

Idx_cache.append ('% s'

% (sec_count, novel_title))

For line in input:

# is a chapter's title?

If line.strip () ='':

Pass

Elif re.match (section_re, line):

Line = re.sub (r'\ s subscription,'', line)

Print 'converting% s..'% line

# write the section footer

Sec_cache.append ('

')

If sec_count = = 0:

Sec_cache.append ('directory |')

Sec_cache.append ('next article |'

% (sec_count + 1))

Else:

Sec_cache.append ('previous article |'

% (sec_count-1))

Sec_cache.append ('directory |')

Sec_cache.append ('next article |'

% (sec_count + 1))

Sec_cache.append ('go back to the top of the page

')

Sec_cache.append ('')

Output.writelines (sec_cache)

Output.flush ()

Output.close ()

Sec_cache = []

Sec_count + = 1

# create a new section

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

Output.writelines ([section_head% (line, line)])

Idx_cache.append ('% s'

% (sec_count, line))

Else:

Sec_cache.append ('% s

'

% escape_xml (line))

# write rest lines

Sec_cache.append ('next article |'

% (sec_count-1))

Sec_cache.append ('directory |')

Sec_cache.append ('go back to the top of the page

')

Output.writelines (sec_cache)

Output.flush ()

Output.close ()

Sec_cache = []

# write the menu

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

Menu_head ='% s directory'% novel_title

Output.writelines ([section_head% (menu_head, menu_head)

''])

Output.writelines (idx_cache)

Output.writelines (['])

Output.flush ()

Output.close ()

Inx_cache = []

Print 'completed. D chapter (s) in total.' sec_count

If _ _ name__ = ='_ _ main__':

Main ()

Thank you for your reading, the above is the content of "how to use the Python splitter", after the study of this article, I believe you have a deeper understanding of how to use the Python splitter, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Divider catalogue learning content preface multiple world Song style novel that is ideas situations documents articles more italics knowledge knowledge points articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Huawei MySQL NVidia