Reputation: 53
I'm trying to write a numpy array to a .csv using numpy.savetxt using a comma delimiter, however it's missing the very first entry (row 1 column 1), and I have no idea why.
I'm fairly new to programming in Python, and this might be simply a problem with the way I'm calling numpy.savetxt or maybe the way I'm defining my array. Anyway here's my code:
import numpy as np
import csv
# preparing csv file
csvfile = open("np_csv_test.csv", "w")
columns = "ymin, ymax, xmin, xmax\n"
csvfile.write(columns)
measurements = np.array([[0.9, 0.3, 0.2, 0.4],
[0.8, 0.5, 0.2, 0.3],
[0.6, 0.7, 0.1, 0.5]])
np.savetxt("np_csv_test.csv", measurements, delimiter = ",")
I expected four columns with 3 rows under the headers ymin, ymax, xmin, and xmax, and I did, but I'm missing 0.9. As in, row 2 column 1 of my .csv is empty, and in Notepad I'm getting:
ymin, ymax, xmin, xmax
,2.999999999999999889e-01,2.000000000000000111e-01,4.000000000000000222e-01
8.000000000000000444e-01,5.000000000000000000e-01,2.000000000000000111e-01,2.999999999999999889e-01
5.999999999999999778e-01,6.999999999999999556e-01,1.000000000000000056e-01,5.000000000000000000e-01
What am I doing wrong?
Upvotes: 5
Views: 1289
Reputation: 13999
When you call np.savetxt
with a path to the output file, it will try to overwrite any existing file, which is not what you want. Here's how you can write your desired file with column headers:
import numpy as np
# preparing csv file
columns = "ymin, ymax, xmin, xmax"
measurements = np.array([[0.9, 0.3, 0.2, 0.4],
[0.8, 0.5, 0.2, 0.3],
[0.6, 0.7, 0.1, 0.5]])
np.savetxt("np_csv_test.csv", measurements, delimiter = ",", header=columns)
As pointed out by Andy in the comments, you can get np.savetxt
to append to an existing file by passing in a file handle instead of a file name. So another valid way to get the file you want would be:
import numpy as np
import csv
# preparing csv file
csvfile = open("np_csv_test.csv", "w")
columns = "ymin, ymax, xmin, xmax\n"
csvfile.write(columns)
measurements = np.array([[0.9, 0.3, 0.2, 0.4],
[0.8, 0.5, 0.2, 0.3],
[0.6, 0.7, 0.1, 0.5]])
np.savetxt(csvfile, measurements, delimiter = ",")
# have to close the file yourself in this case
csvfile.close()
Upvotes: 1