How to save xarray data to csv

Rhodri James rhodri at kynesim.co.uk
Mon Apr 16 13:50:45 EDT 2018


On 16/04/18 15:55, shalu.ashu50 at gmail.com wrote:
> Hello All,
> 
> I have used xarray to merge several netcdf files into one file and then I subset the data of my point of interest using lat/long. Now I want to save this array data (time,lat,long) into csv file but I am getting an error with my code:


You don't say, but I assume you're using Python 2.x

[snip]

> # xarray to numpy array
> clt1=numpy.array(clt0sub)
> # saving data into csv file
> with open('combine11.csv', 'wb') as f:
>      writer = csv.writer(f, delimiter=',')
>      writer.writerows(enumerate(clt1))
> 
> getting this error - TypeError: a bytes-like object is required, not 'str'

Copy and paste the entire traceback please if you want help.  We have 
very little chance of working out what produced that error without it.

> when I am removing "b" the error disappears

Which "b"?  Don't leave us guessing, we might guess wrong.

> but the data saving in wrong format

Really?  It looks to me like you are getting exactly what you asked for. 
  What format were you expecting?  What are you getting that doesn't 
belong.  I suspect that you don't want the "enumerate", but beyond that 
I have no idea what you're after.

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list