用多列写excel文档

嗨,大家好我在这里得到一个奇怪的错误,不知道是什么问题。 不幸的是有太多的可能性,我无法弄清楚。我有一些时间string,我从数据库中返回。 我想把它们放进一个带有一些随机生成值的Excel电子表格中。 任何想法将不胜感激:)

import xlsxwriter # Create a workbook and add a worksheet. workbook = xlsxwriter.Workbook('Expenses042.xlsx') worksheet = workbook.add_worksheet() # Add a bold format to use to highlight cells. bold = workbook.add_format({'bold': True}) # Write some data headers. worksheet.write('A1', 'Facility', bold) worksheet.write('B1', 'Type', bold) # Some data we want to write to the worksheet. expenses=[] for T in list1: FM = random.randint(0,13) RUL = random.randint(1,365) SL = random.randint(0,4) S = str(T['value']) expenses= (['rent', S]) print expenses # Start from the first cell below the headers. row = 1 col = 0 # Iterate over the data and write it out row by row. for item, cost in (expenses): worksheet.write(row, col, item) worksheet.write(row, col + 1, cost, money) row += 1 workbook.close() 

OUTPUT

 runfile('C:/Users/123/Documents/emerson/CSVFILE.py', wdir='C:/Users/123/Documents/emerson') Extracting all files from MongoDB ['rent', '24-08-2016 14:59:08'] ['rent', '24-08-2016 14:59:08'] ['rent', '21-08-2016 6:59:08'] ['rent', '21-08-2016 6:59:08'] ['rent', '22-08-2016 4:59:09'] ['rent', '22-08-2016 4:59:09'] ['rent', '23-08-2016 7:59:12'] ['rent', '23-08-2016 7:59:12'] ['rent', '21-08-2016 7:55:40'] ['rent', '21-08-2016 7:55:40'] ['rent', '24-08-2016 7:57:06'] ['rent', '24-08-2016 7:57:06'] ['rent', '24-08-2016 16:56:49'] ['rent', '24-08-2016 16:56:49'] ['rent', '23-08-2016 8:56:59'] ['rent', '23-08-2016 8:56:59'] ['rent', '21-08-2016 12:55:38'] ['rent', '21-08-2016 12:55:38'] ['rent', '24-08-2016 23:59:16'] ['rent', '24-08-2016 23:59:16'] ['rent', '24-08-2016 14:59:08'] ['rent', '24-08-2016 14:59:08'] ['rent', '21-08-2016 6:59:08'] ['rent', '21-08-2016 6:59:08'] ['rent', '22-08-2016 4:59:09'] ['rent', '22-08-2016 4:59:09'] ['rent', '23-08-2016 7:59:12'] ['rent', '23-08-2016 7:59:12'] ['rent', '21-08-2016 7:55:40'] ['rent', '21-08-2016 7:55:40'] ['rent', '24-08-2016 7:57:06'] ['rent', '24-08-2016 7:57:06'] ['rent', '24-08-2016 16:56:49'] ['rent', '24-08-2016 16:56:49'] ['rent', '23-08-2016 8:56:59'] ['rent', '23-08-2016 8:56:59'] ['rent', '21-08-2016 12:55:38'] ['rent', '21-08-2016 12:55:38'] ['rent', '24-08-2016 23:59:16'] ['rent', '24-08-2016 23:59:16'] 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 0x0AD755B0>> ignored Traceback (most recent call last): File "<ipython-input-132-bfb0167086a1>", line 1, in <module> runfile('C:/Users/123/Documents/emerson/CSVFILE.py', wdir='C:/Users/123/Documents/emerson') File "C:\Users\123\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile execfile(filename, namespace) File "C:\Users\123\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/123/Documents/emerson/CSVFILE.py", line 237, in <module> for item, cost in (expenses): ValueError: too many values to unpack 

每次都会在循环中覆盖dates 。 所以当你在列表上迭代时,它不起作用,因为它仅仅是一个列表,而不是一对夫妇的列表。 第一次迭代产生"date" ,不能被解压缩成2个variables。

首先定义dates列表为空,然后为每个迭代追加列表。

 expenses = [] # you're doing that all right # Some data we want to write to the worksheet. for T in list1: ... expenses.append(['date', 1234]) 

现在你可以迭代夫妻并解开价值。