【问题标题】:Python openpyxl write list to Excel errorPython openpyxl将列表写入Excel错误
【发布时间】:2015-07-06 00:13:34
【问题描述】:

我是 Python 新手,正在做我的第一个项目。我试图让我的代码从一个电子表格中复制数据列并将其附加到当前存在于主工作表中的数据中。我能够捕获每张工作表中的数据并创建一个结合两个数据集的新主列表,但我无法将其写入文件。当我测试打印组合列表时,它们看起来是正确的,但是当我添加代码以将列表写入文件时,它会挂起。

您能提供的任何帮助都会非常有帮助!

下面是我的代码。这是我遇到的错误

Traceback(最近一次调用最后一次):文件 “/Path/Path/Path/extractData.py”,第 50 行,在 destSheet.cell(row=rowNum, column=1).value = masterList1

文件 "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/openpyxl/cell/cell.py", 第 313 行,价值 self._bind_value(value)

文件 "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/openpyxl/cell/cell.py", 第 217 行,在 _bind_value 中 raise ValueError("Cannot convert {0} to Excel".format(value))

ValueError: 无法转换 ['Reinsurer', 'Market 1', 'Market 2', '市场 3'、'市场 4'、'市场 5'、'市场 1'、'市场 2'、'市场 3', '市场 4', '市场 5', ['市场 1', '市场 2', '市场 3', '市场 4'、'市场 5']] 到 Excel

import openpyxl

#Open source data file
sourceFile = openpyxl.load_workbook('/Path/Path/Path/testAuth.xlsx')
sourceSheet = sourceFile.get_sheet_by_name('Sheet1')

#read data from source file and create lists of new data
newList1 = []
newList2 = []
newList3 = []

for row in range(2, sourceSheet.get_highest_row()):
    data1 = sourceSheet['A' + str(row)].value
    newList1.append(data1)
    data2 = sourceSheet['B' + str(row)].value
    newList2.append(data2)
    data3 = sourceSheet['C' + str(row)].value
    newList3.append(data3)

#open destination workbook that includes the master database
destFile = openpyxl.load_workbook('/Path/Path/Path/testHist.xlsx')
destSheet = destFile.get_sheet_by_name('Sheet1')

#create empty lists for copying the historical data already in the workbook
masterList1 = []
masterList2 = []
masterList3 = []

#grab master spreadsheet data and write to list
for row in range(1, destSheet.get_highest_row()+1):
    masterData1 = destSheet['A'+ str(row)].value
    masterList1.append(masterData1)
    masterData2 = destSheet['B'+ str(row)].value
    masterList2.append(masterData2)
    masterData3 = destSheet['C'+ str(row)].value
    masterList3.append(masterData3)

#append new data to the history list
masterList1.append(newList1)
masterList2.append(newList2)
masterList3.append(newList3)

#write new master list to a new file
for rowNum in range(2, len(masterList1)):
    destSheet.cell(row=rowNum, column=1).value = masterList1

destSheet.save("updatedTest.xlsx")

【问题讨论】:

    标签: python excel openpyxl


    【解决方案1】:

    您可能打算使用masterList1[rowNum] 作为值,而不是将每个单元格的值分配给masterList1

    for rowNum in range(2, len(masterList1)):
        destSheet.cell(row=rowNum, column=1).value = masterList1[rowNum]
    

    另外,正如@mgrant 指出的那样,您之前应该扩展(未附加)主列表:

    masterList1.extend(newList1)
    

    【讨论】:

    • 感谢 alecxe 和 mgrant。这两个答案都让我走上了正轨。对上面的建议有一个小的更正。使用扩展需要以下语法:masterList1.extend([newList])。当不使用方括号时,我的值被分成单个字符,而不是完整的单元格值。
    • @RFuller extend 将序列作为参数。字符串是单个字符的序列。这是 Python 中的常见问题。
    【解决方案2】:

    您是否尝试过使用extend 而不是appendappend 将单个项目添加到列表的末尾,而 extend 接受一个列表参数并添加每个项目。

    【讨论】:

    • openpyxl 工作表不是列表。他们确实有一个append 方法,它类似于列表的append 方法,但具有特定的语法和语义。尝试将列表分配给单个单元格时会引发异常。
    • @CharlieClark,你是对的。我的意思是说@alecxe 指出,在构建masterList1 等时需要extend。但我同意分配是真正的问题。
    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 2023-03-31
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2018-08-04
    相关资源
    最近更新 更多