Python Win32 COM MemoryError:正在创建尝试将数据插入excel的安全阵列

Python Win32 COM MemoryError:正在创建尝试将数据插入excel的安全阵列,python,numpy,pywin32,win32com,comtypes,Python,Numpy,Pywin32,Win32com,Comtypes,我试图通过以下调用将列表列表插入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浮点和整数。当我尝试执行上述调用时,出现以下错误: Traceback (most r

我试图通过以下调用将列表列表插入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浮点和整数。当我尝试执行上述调用时,出现以下错误:

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
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python32\lib\site packages\win32com\client\dynamic.py”,第570行,位于\uuu setattr中__
self._oleobj_uj.Invoke(entry.dispid,0,Invoke_类型,0,值)
MemoryError:创建安全数组

导致此win32com.client
内存错误的原因是什么?谢谢

我确定问题在于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
>>>
>#使用2个numpy float64值初始化测试列表
>>>test=[numpy.float64(456),numpy.float64(456)]
>>>#尝试插入仅包含numpy数据类型的列表将出错
>>>ws.Range(ws.Cells(1,1),ws.Cells(1,2)).value=test
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python32\lib\site packages\win32com\client\dynamic.py”,第570行,位于\uuu setattr中__
self._oleobj_uj.Invoke(entry.dispid,0,Invoke_类型,0,值)
MemoryError:创建安全数组
>>>#将其中一个值更改为任何其他标准python数据类型将允许插入列表
>>>测试[1]=“测试”
>>>ws.Range(ws.Cells(1,1),ws.Cells(1,2)).value=test
#具有多个numpy数据类型的列表在插入时将出错
>>>test.append(numpy.int64(456))
>>>ws.Range(ws.Cells(1,1),ws.Cells(1,3)).value=test
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python32\lib\site packages\win32com\client\dynamic.py”,第570行,位于\uuu setattr中__
self._oleobj_uj.Invoke(entry.dispid,0,Invoke_类型,0,值)
MemoryError:创建安全数组
>>>“”“结论:只有当所有numpy数据类型都相同并且列表中也有内置数据类型时,才能插入列表”“”
>>>测试[2]=numpy.float64(测试[2])
>>>ws.Range(ws.Cells(1,1),ws.Cells(1,3)).value=test
>>>

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

当您只传递单个值
None
时,也会发生这种情况,因为它出乎意料地不是您认为的浮点值。