道歉爲我的長描述。我沒有任何選擇...錯誤在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")
請扔在如何糾正錯誤的一些情況。
它看起來像您嘗試讀取的文件xlsx文件不在您運行程序的目錄中。 – jmcnamara
但我在閱讀之前創建工作簿並仍然出現錯誤。 – user2212100
嗨jmcnamara,請告訴我如何繼續前進 – user2212100