gpt4 book ai didi

python - win32com 内存错误 : CreatingSafeArray attempting to insert data into excel

转载 作者:太空狗 更新时间:2023-10-30 00:36:26 24 4
gpt4 key购买 nike

我正在尝试使用以下调用将列表列表插入到 excel 中(这样每个内部列表代表一行,每行长度相同):

#Assume ws is correctly initialized to an excel worksheet object
ws.Range(ws.Cells(1,1),ws.Cells(len(myList),len(myList[0]))).value = myList

myList 列表包含字符串和 numpy float 和整数。当我尝试执行上面的调用时出现以下错误:

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
MemoryError: CreatingSafeArray

是什么导致了这个 win32com.client MemoryError?谢谢!

最佳答案

我确定问题出在 numpy 值上:

>>> #Initialize test list with 2 numpy float64 values
>>> test = [numpy.float64(456),numpy.float64(456)]
>>> #Attempting to insert a list containing only numpy data types will error
>>> ws.Range(ws.Cells(1,1),ws.Cells(1,2)).value = test
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
MemoryError: CreatingSafeArray
>>> #Changing one of the values to any other standard python data type will allow the list to be inserted
>>> test[1] = 'test'
>>> ws.Range(ws.Cells(1,1),ws.Cells(1,2)).value = test
# A list with multiple numpy data types will error upon insertion
>>> test.append(numpy.int64(456))
>>> ws.Range(ws.Cells(1,1),ws.Cells(1,3)).value = test
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
MemoryError: CreatingSafeArray
>>> """ Conclusion: A list can be inserted only if all of the numpy data types are the same and there is a built-in data type in the list as well """
>>> test[2] = numpy.float64(test[2])
>>> ws.Range(ws.Cells(1,1),ws.Cells(1,3)).value = test
>>>

我的解决方案是在插入之前将列表中的所有值简单地转换为字符串,保证没有数据类型会给我带来问题。

关于python - win32com 内存错误 : CreatingSafeArray attempting to insert data into excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18136870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com