本文整理匯總了Python中zipfile.PyZipFile.read方法的典型用法代碼示例。如果您正苦於以下問題:Python PyZipFile.read方法的具體用法?Python PyZipFile.read怎麽用?Python PyZipFile.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zipfile.PyZipFile
的用法示例。
在下文中一共展示了PyZipFile.read方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: EPub
# 需要導入模塊: from zipfile import PyZipFile [as 別名]
# 或者: from zipfile.PyZipFile import read [as 別名]
class EPub(object):
epub_file = None
epub_zip = None
title = None
def __init__(self, epub_file):
if not epub_file:
raise ValueError('Invalid epub file path')
self.epub_file = epub_file
self.epub_zip = PyZipFile(epub_file, 'r')
def table_of_contents(self):
basedir = None
# find opf file
soup = BeautifulSoup(self.epub_zip.read('META-INF/container.xml'))
opf = dict(soup.find('rootfile').attrs)['full-path']
basedir = os.path.dirname(opf)
if basedir:
basedir = '{0}/'.format(basedir)
soup = BeautifulSoup(self.epub_zip.read(opf), "html.parser")
# title
self.title = soup.find('dc:title').text
# all files, not in order
x, ncx = {}, None
for item in soup.find('manifest').findAll('item'):
d = dict(item.attrs)
x[d['id']] = '{0}{1}'.format(basedir, d['href'])
if d['media-type'] == 'application/x-dtbncx+xml':
ncx = '{0}{1}'.format(basedir, d['href'])
book_sections = []
for item in soup.find('spine').findAll('itemref'):
book_sections.append(x[dict(item.attrs)['idref']])
nav_labels = {}
if ncx:
soup = BeautifulSoup(self.epub_zip.read(ncx), "html.parser")
for navpoint in soup('navpoint'):
k = navpoint.content.get('src', None)
# strip off any anchor text
k = k.split('#')[0]
if k:
nav_labels[k] = navpoint.navlabel.text
toc = []
for section in book_sections:
if section in nav_labels:
toc.append(nav_labels[section])
else:
toc.append(section)
return toc
def chapter_to_text(self, chapter):
soup = BeautifulSoup(self.epub_zip.read(chapter), "html.parser")
return soup.find('body').getText()
def book_parts(self):
toc = self.table_of_contents()
book = []
for chapter in toc:
book.append((chapter, self.chapter_to_text(chapter)))
return book