我正在尝试使用模块xlsxwriter从列表生成excel工作表,但我不断收到工作簿不会关闭的错误。这是错误消息和代码。该列表正确生成数据。
代码:
import csv
import xlsxwriter
f = open('alerts2.csv')
csv_f = csv.reader(f)
li1 = []
header = next(csv_f)
for row in csv_f:
# li1.append(row[5])
# li1.append(row[0])
severity = int(row[0]) #Has The the integer value from 10 - 40
if severity != 40:
continue
Status = str(row[1])
PolicyName = str(row[2])
PolicyBlockName = str(row[3])
PolicyRuleName = str(row[4])
Summary = str(row[5])
li1.append(severity)
li1.append(Summary) # string variables
print li1
f.close()
print len(li1)
#creating a workbook and a worksheet
workbook = xlsxwriter.Workbook('results_stuff.xlsx')
worksheet = workbook.add_worksheet()
#initializing variables called row and columns for inputting
row = 0
col = 0
#forloop iteration over the data and writing it in row by row
for severity_level, summary_description in (li1):
worksheet.write(row, col, severity_level)
worksheet.write(row, col +1, summary_description)
row += 1
workbook.close()
错误消息:
traceback (most recent call last):
File "parse.py", line 33, in <module>
for severity_level, summary_description in (li1):
TypeError: 'int' object is not iterable
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 0x1054fefd0>> ignored