Skip to content

Instantly share code, notes, and snippets.

@shuyo
Last active January 24, 2017 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shuyo/5394863 to your computer and use it in GitHub Desktop.
Save shuyo/5394863 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs, sys, zipfile
CODE = "shift_jis"
firstpage = True
g = codecs.open("output.txt", "wb", CODE)
files = sys.argv[1:]
isSingle = True
if files[0] == "-t":
title = files[1].decode(CODE)
files = files[2:]
isSingle = False
for filename in files:
if filename.lower().endswith(".zip"):
z = zipfile.ZipFile(filename)
targets = filter(lambda x:x.endswith(".txt"), z.namelist())
if len(targets) == 0:
sys.err.write("No .txt file in %s\n" % filename)
continue
f = codecs.iterdecode(z.open(targets[0]), CODE)
else:
f = codecs.open(filename, "rb", CODE)
if isSingle:
title = f.next()
chapter = f.next()
author = f.next()
body = ""
for s in f:
if s.startswith("-------------------------------------------------------"):
body = ""
elif s.startswith(u"底本:"):
break
else:
body += s
if firstpage:
g.write(title)
g.write("\n")
g.write(author)
g.write("\n")
firstpage = False
g.write(u"\n[#改ページ]\n")
g.write(chapter)
g.write("\n")
g.write(body)
f.close()
g.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment