简体   繁体   English

win32com MemoryError:CreatingSafeArray试图将数据插入excel

[英]win32com MemoryError: CreatingSafeArray attempting to insert data into excel

I'm attempting to insert a list of lists into excel (such that each inner list represents a row, each is of the same length) with the following call : 我试图通过以下调用将一个列表列表插入到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 lists contain strings and numpy floats and ints. myList列表包含字符串和numpy浮点数和整数。 I get the following error when I attempt to execute the call above: 当我尝试执行上面的调用时出现以下错误:

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

what is causing this win32com.client MemoryError ? 导致这个win32com.client MemoryError原因是什么? Thanks! 谢谢!

I determined the problem was with the numpy values: 我确定问题出在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
>>>

My solution was to simply convert all values in my list to string before inserting, guaranteeing no data types will give me problems. 我的解决方案是在插入之前简单地将列表中的所有值转换为字符串,保证没有数据类型会给我带来问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM