簡體   English   中英

win32com MemoryError:CreatingSafeArray試圖將數據插入excel

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

我試圖通過以下調用將一個列表列表插入到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

導致這個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
>>>

我的解決方案是在插入之前簡單地將列表中的所有值轉換為字符串,保證沒有數據類型會給我帶來問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM