Out of memory while reading excel file

Peter Otten __peter__ at web.de
Thu May 11 02:46:17 EDT 2017


Mahmood Naderan via Python-list wrote:

>>a = numpy.zeros((ws.max_row, ws.max_column), dtype=float)
>>for y, row in enumerate(ws.rows):
>>   a[y] = [cell.value for cell in row]
> 
> 
> 
> Peter,
> 
> As I used this code, it gave me an error that cannot convert string to
> float for the first cell. All cells are strings.

For string values you have to adapt the dtype:

a = numpy.empty((ws.max_row, ws.max_column), dtype=object)
for y, row in enumerate(ws.rows):
    a[y] = [cell.value for cell in row]

If that completes and is fast enough, fine.

But again, for non-numeric data numpy doesn't make much sense IMHO -- if you 
tell us what you're up to we may be able to suggest a better approach.




More information about the Python-list mailing list