'Writing to CSV file with numpy arrays without brackets

I'm attempting to map out coordinates for a circle and export those values to excel to then plot the values. My output CSV file returns the correct values but with brackets around the values ex x,y, [0.],[9.]. I would like to learn how to remove those brackets with my current code attempt if possible.

import numpy as np
from itertools import zip_longest

r = 9
h = 1
k = 1

# r = int(input('Radius'))
# h = int(input('h'))
# k = int(input('k'))

deg = np.arange(0, 361, 1)[:, np.newaxis]
theta = (np.pi * deg * 2)/360

x = (r * np.sin(theta) + h)
y = (r * np.cos(theta) + k)
d = x, y

Points = zip_longest(*d, fillvalue = '')
with open('test.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
    wr = csv.writer(myfile)
    wr.writerow(('x','y','r','h','k'))
    wr.writerows(Points)
myfile.close()


Solution 1:[1]

x and y are arrays with shape (361,1). When you iterate over such an array (as zip_longest will do), you get a stream of arrays of shape (1,) which is where your surplus brackets come from.

One solution would be to make your arrays (361,) instead (i.e, one-dimensional). Then iterating would give scalars.

Another solution would be to use np.hstack([x,y]) instead of zip, and tossing the entire resulting (361,2) array to writerows.

(Off topic: You do not need myfile.close(); the with ... construct takes care of that for you — that's what it's for!)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ture Pålsson