Writing NDArray to JSON and .CV Files

I have an ndarray sized 9742 rown x 26 columns. It has types such as date, integers, floats, etc. And the column headings, such as "Date", "Amount", "Signs", etc. .... The fact is that I wanted to save it line by line to another file. I was hoping I could help me with this.

I tried using:

for k1 in range(1,len(arr)): c.writerow([arr[index[1:27]][k1]]) 

But that gives me an unshakable type error. index is a python map that I use to scroll through column headings, as in 1: Date, 2: Amount, etc ...

I would also like to write it to a JSON file. However, I have no experience with JSON. I would really appreciate it if you could help me with this.

+4
source share
1 answer

You might be better off using numpy.savetext() instead of writing each line manually. In this case you can use:

 numpy.savetxt(filename, arr, header=arr.dtype.names) 

You can also try converting the thing you write to a string using something like:

 for arr_row in arr: c.writerow(" ".join(map(str, arr_row))) 
0
source

All Articles