-
Notifications
You must be signed in to change notification settings - Fork 0
/
Writer.py
48 lines (43 loc) · 1.7 KB
/
Writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
import os
class Writer(object):
def __init__(self,length):
self.datas =list(range(0,length))
self.title=""
def collect_data(self, data,title):
if data is None:
return
self.datas[data['section_title']]=data
self.title=title
def output_html_old(self):
try:
os.mkdir("book/"+self.title)
except:
print("该书目录已存在".decode('utf-8'))
i=0
for data in self.datas:
if os.path.exists("book/"+self.title + "/" +"%d"%data['section_title']+".txt"):
print("book/"+self.title + "/" +"%d"%data['section_title']+".txt"+"已存在".decode('utf-8'))
pass
else:
try:
fout = open("book/"+self.title + "/" +"%d"%data['section_title']+".txt", 'a')
fout.write("%s" %data['text'])
except:
print("%d"%data['section_title']+"写入失败".decode('utf-8'))
fout.close()
def output_html(self):
try:
os.mkdir("C:/book")
except:
print("书包目录已存在".decode('utf-8'))
if os.path.exists("C:/book/" + self.title + ".txt"):
print(self.title+"已下载。。。".decode('utf-8'))
else:
fout = open("C:/book/" + self.title + ".txt", 'a')
for data in self.datas:
try:
fout.write("第"+str(data['section_title'])+"章")
fout.write("%s"%data['text'])
except:
print(self.title+"写入失败!--".decode('utf-8'))