zip中的多个文本文件分析

时间:2015-07-11 13:59:59

标签: python

这是一个程序 <h:panelGroup layout="block" class="box-header"> <h3 class="box-title">Branches</h3> &nbsp;&nbsp; <h:commandButton value="Add New Branch" id="createBranchBtn" pt:data-loading-text="Loading..." pt:autocomplete="off" class="btn btn-primary sye-action-btn-loading"> <f:param name="pageType" value="create"/> </h:commandButton> </h:panelGroup> <h:panelGroup layout="block" class="box-body table-responsive no-padding branches-datatable"> <div class="SYE-modal"> </div> <h:panelGroup id="branchesListDiv" layout="block" class="container-fluid"> <h:dataTable id="example1" binding="#{index}" class="table table-hover table-bordered table-striped" value="#{branchesMB.list}" var="branch"> <h:column> <f:facet name="header" > <h:outputText value="#"/> </f:facet> <h:outputText value="#{index.rowIndex+1}"/> <f:facet name="footer" > <b><h:outputText value="#"/></b> </f:facet> </h:column> <h:column > <f:facet name="header"> <h:outputText value="Branch Name"/> </f:facet> <h:outputText value="#{branch.branchName}"/> <f:facet name="footer"> <b><h:outputText value="Branch Name"/></b> </f:facet> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Address"/> </f:facet> <h:outputText value="#{branch.branchAddress}"/> <f:facet name="footer"> <b><h:outputText value="Address"/></b> </f:facet> </h:column> <h:column> <f:facet name="header"> <h:outputText value="Abbreviation"/> </f:facet> <h:outputText value="#{branch.branchAbbreviation}"/> <f:facet name="footer"> <b><h:outputText value="Abbreviation"/></b> </f:facet> </h:column> <h:column headerClass="syeActionDatatable"> <f:facet name="header"> <h:outputText value="Action"/> </f:facet> <f:facet name="footer"> <b><h:outputText value="Action"/></b> </f:facet> <div class="btn-group" > <a class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" href="#"> <i class="glyphicon glyphicon-cog"></i> <span class="caret"></span> </a> <ul class='dropdown-menu pull-right'> <li> <!--pt:data-toggle="modal" pt:data-target=".bs-example-modal-lg"--> <h:commandLink actionListener="#{branchesMB.setEntityEditObject(branch)}" class="editDatatable" > <f:ajax onevent="load" onerror="load" render=":branchesForm:branchEditArea"/> <i class='glyphicon glyphicon-pencil'></i> Edit </h:commandLink> </li> <li> <!--onclick="if (!confirm('Are you sure you want to delete this record?')) return false"--> <h:commandLink action="#{branchesMB.destroy()}" onclick="if (!confirm('Are you sure you want to delete this record?')) return false;"> <f:ajax render=":branchesForm:branchEditArea"/> <f:setPropertyActionListener target="#{branchesMB.entityDeleteObject}" value="#{branch}"/> <i class='glyphicon glyphicon-trash'></i> Delete </h:commandLink> </li> </ul> </div> </h:column> </h:dataTable> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel">Edit Branch</h4> </div> <div class="modal-body"> <h:panelGroup id="branchEditArea" layout="block" class="box-body"> <ui:include src="/view/includes/messages.xhtml"/> <h:inputHidden value="#{branchesMB.entityEditObject}" converter="BranchesConverter"/> <h:panelGroup id="branchNameDiv" layout="block" class="form-group"> <h:outputLabel value="Branch Name" for="branchName"/> <h:inputText id="branchName" class="form-control" value="#{branchesMB.entityEditObject.branchName}" /> </h:panelGroup> <h:panelGroup id="branchAddressDiv" layout="block" class="form-group"> <h:outputLabel value="Branch Address" for="branchAddress"/> <h:inputText id="branchAddress" class="form-control" value="#{branchesMB.entityEditObject.branchAddress}"/> </h:panelGroup> <h:panelGroup id="branchAbbreviationDiv" layout="block" class="form-group"> <h:outputLabel value="Abbreviation" for="branchAbbreviation"/> <h:inputText id="branchAbbreviation" class="form-control" value="#{branchesMB.entityEditObject.branchAbbreviation}"/> </h:panelGroup> </h:panelGroup> </div> <div class="modal-footer"> <!--pt:data-dismiss="modal"--> <h:commandButton id="close" value="Close" class="btn btn-default" > <f:ajax render="branchesForm:branchesListDiv"/> </h:commandButton> <h:commandButton id="edit" value="Edit" action="#{branchesMB.update()}" class="btn btn-primary sye-action-btn-loading" pt:data-loading-text="Loading..." pt:autocomplete="off"> <f:ajax render=":branchesForm:branchesListDiv,:branchesForm:branchEditArea, @this" execute="branchEditArea branchesForm:branchesListDiv"/> </h:commandButton> <!-- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button>--> </div> </div> </div> </div> </h:panelGroup> </h:panelGroup> <h:panelGroup layout="block" class="box-footer"> Branches </h:panelGroup> </h:form> <ui:define name="scriptPlugins"> <h:panelGroup id="scriptPlugins"> <script src="#{request.contextPath}/resources/SYE-2.1.1/plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script> <script src="#{request.contextPath}/resources/SYE-2.1.1/plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script> </h:panelGroup> </ui:define> ,它将打印一个zip文件的包含。假设,Zip包含多个文本文件,因此它将打印文本文件的名称。我很好。

printzip.py
  

输出: - test1.txt,test2.txt

但是,我的问题是,如何将此文本文件用于进一步分析。 就像,我想计算那些文本文件的行,单词,字符(我可以执行这些操作)。

但是,我的问题是如何将此输出作为变量用于输入更多工作。因为我们不知道文本文件的名称。它可以是单个或多个。

作为一个例子,我想将test1.txt和test2.txt作为未来操作的输入。

另外,在运行import zipfile zf = zipfile.ZipFile('Desktop.zip', 'r') print (zf.namelist()) 程序之前,我们不知道zip文件包含这两个文本文件。这个多处理工作有没有库?感谢。

1 个答案:

答案 0 :(得分:1)

zf.namelist()返回存档中文件名称的Python列表。您可以使用for循环从该列表中获取每个名称,并在循环中使用zf.read()来获取每个文件的字节。例如,这将打印名称列表,然后是名称&amp;存档中每个文件的未压缩大小。

import zipfile
zf = zipfile.ZipFile('Desktop.zip', 'r')
fnames = zf.namelist()
print(fnames)
for fname in fnames:
    data = zf.read(fname)
    print(fname, len(data))
    #... do other text processing here

zf.close()