How to save xarray data to csv

shalu.ashu50 at shalu.ashu50 at
Mon Apr 16 22:25:54 EDT 2018

On Tuesday, 17 April 2018 01:56:25 UTC+8, Rhodri James  wrote:
> On 16/04/18 15:55, shalu.ashu50 at 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

Hi James, I am using WinPython Spyder 3.6. 
> [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
here i mean [with open('combine11.csv', 'wb') as f:] wb: writing binaries
if i am using "wb" so i m getting "TypeError: a bytes-like object is required, not 'str'"

if i am removing "b" and using only "w" so this error disappears and when i am writing data into txt/csv so it is just pasting what i am seeing in my console window. I mean i have 20045 time steps but i am getting that as previously mentioned. Not getting full time steps. It is like printscreen of my python console.

My question is how can i save multi-dimentional (3d: time series values, lat, long) data (xarrays) into csv. 


> 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