2015-08-27 64 views
1

道歉为我的长描述。我没有任何选择...错误在Xlrd模块

基本上,我已经写了一个XML解析器在python中。它从XML标记中提取数据并使用xlsxwriter模块将数据写入Excel工作表。一旦创建了Excel工作表,我使用xlrd包在Excel中读取数据,处理数据,然后根据结果列中相同的Excel中的条件更新我的结果。现在,我的问题是,当Excel已经创建并出现在路径中我的程序工作正常,但是当我删除Excel并运行该程序时,它会抛出如下错误:

Traceback(最近一次调用最后):

File "AMS_parse.py", line 71, in <module> 
rworkbook = xlrd.open_workbook('AMS.xlsx') 
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 394, 
    in open_workbook 
f = open(filename, "rb") 
IOError: [Errno 2] No such file or directory: 'AMS.xlsx' 
Exception Exception: Exception('Exception caught in workbook destructor. 
Explicit close() may be required for workbook.',) 
    in <bound method Workbook.__del__ of 
    <xlsxwriter.workbook.Workbook object at 0x0297E450> 

我的程序如下:

import xml.etree.ElementTree as ET 
import xlsxwriter 
import xlrd 
# create a work book AMS.xlsx 
workbook = xlsxwriter.Workbook('AMS.xlsx') 
# create a worksheet "AMSLogging" in work book AMS.xlsx 
worksheet = workbook.add_worksheet("AMSLogging") 
# format the work sheet 
format = workbook.add_format({'bold': 25,'font_color': 'green'}) 
worksheet.write('A1','Component',format) 
worksheet.write('B1','DataType',format) 
worksheet.write('C1','PrivateData',format) 
worksheet.write('D1','Result',format) 
worksheet.set_column('A:A', 15) 
worksheet.set_column('B:B', 25) 
worksheet.set_column('C:C', 40) 
worksheet.set_column('D:D', 50) 
#create a tree for AMS.xml 
tree = ET.ElementTree(file='AMS.xml') 
root = tree.getroot() 
print "PLEASE CHECK THE RESULT IN THE SAME WORKING DIRECTORY OF THE FILE AMS.XLSX AND AMSMISCLOGGING.TXT" 
#set the row to 1'st row since 0th row is Headline 
row = 1 
#set the column to zero'th column 
col = 0 
#get the Component values from the XML 
for Component in tree.iter(tag='Component'):  
    worksheet.write_string(row,col,Component.text) 
    row = row+1 
#reset row to 1st row to start it from the first row for writing Datatype values 
row = 1 
for DataType in tree.iter(tag='DataType'): 
    worksheet.write_string(row,col+1,DataType.text) 
    row = row+1 
#reset row to 1st row to start it from the begining for writing PrivateData values 
row = 1 
for PrivateData in tree.iter(tag='PrivateData'): 
    worksheet.write_string(row,col+2,PrivateData.text) 
    row = row+1 

#--------------------------------------------------------------- 
#--------------------------------------------------------------- 
rworkbook = xlrd.open_workbook('AMS.xlsx') 
sheet = rworkbook.sheet_by_index(0) 
rrow = sheet.nrows 
ccol = sheet.ncols 
print "row:%d column:%d" %(rrow,ccol) 

# set the row to 1 to start from the first row 
row=1 
x = 1 

while x < rrow: 
    for y in range(ccol): 
     if sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0301': 
      worksheet.write(row,col+3,"PF banner is launched") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0302': 
      worksheet.write_string(row,col+3,"Guide is launched") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0402': 
      worksheet.write_string(row,col+3,"Guide is Dismissed") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0401': 
      worksheet.write_string(row,col+3,"PF banner is Dismissed") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0316': 
      worksheet.write_string(row,col+3,"SHOWCASE Menu is launched") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0416': 
      worksheet.write_string(row,col+3,"SHOWCASE Menu is Dismissed") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0317': 
      worksheet.write_string(row,col+3,"ACTVE Menu is Launched") 
     elif sheet.cell_value(x,y) == '1' and sheet.cell_value(x,y+1) == '1' and sheet.cell_value(x,y+2) == '0417': 
      worksheet.write_string(row,col+3,"ACTVE Menu is Dismissed") 
    row = row+1 
    x = x+1 

#Close the work book after writing 
try: 
    workbook.close() 
except: 
    # Handle your exception here. 
    print("please close the AMS.xlsx file") 

请扔在如何纠正错误的一些情况。

+0

它看起来像您尝试读取的文件xlsx文件不在您运行程序的目录中。 – jmcnamara

+0

但我在阅读之前创建工作簿并仍然出现错误。 – user2212100

+0

嗨jmcnamara,请告诉我如何继续前进 – user2212100

回答

1

您需要先关闭workbook,然后再打开rworkbook。 事实上,你不应该使用读卡器,直接在workbook进行计算。

+0

嗨Ruben,当我在阅读之前关闭工作簿之后,我将无法将数据写入工作簿,这是我在Belwo代码中执行的操作:x user2212100