4

I have an ndarray of size 9742 rown x 26 columns. It has types like date, integers, floats etc. and column headers like "Date", "Amount","Marks" and so on.... The thing is, I wanted to save it row by row into another file. I was hoping if you'll could possibly help me with doing this.

I tried using:

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

But it gives me an unhashable type error. index is a python map which I use to loop through the column headings, as in 1: "Date", 2: "Amount" and so on...

I also would like to possibly write it to a JSON file. I have no experience with JSON though. I'd much appreciate it if you'll could please help me with it.

4

1 に答える 1

0

numpy.savetext()各行を手動で記述しようとする代わりに、を使用した方がよい場合があります。その場合、次を使用できます。

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

次のようなものを使用して、書いているものを文字列に変換することもできます。

for arr_row in arr:
    c.writerow(" ".join(map(str, arr_row)))
于 2013-02-19T14:50:01.920 に答える