This answer is for Python3 only. The csv module has a very different interface between Python2 and Python3 and writing compatible code is beyond what I am ready to do here.
Here, I would compute the fieldnames
list, and compute each row on the same pattern:
...
with open(r'C:UsersDesktopkar_csv_testworkfilesoutcsv.csv','w', newline='') as wf:
fieldnames = ['id'] + ['x{}(point_{})'.format(i, j)
for i in range(1, 6) for j in "xyz"] # only up to x5 here
my_write = csv.DictWriter(wf, fieldnames = fieldnames, delimiter = ',')
my_write.writeheader()
for k, v in my_dict.items():
row = {'x{}(point_{})'.format(i, k):
v.get('x{}'.format(i), ('','',''))[j] # get allows to get a blank triple is absent
for i in range(1,6) for j,k in enumerate("xyz")}
row['id'] = k # do not forget the id...
my_write.writerow(row)
With your example data, it gives:
id,x1(point_x),x1(point_y),x1(point_z),x2(point_x),x2(point_y),x2(point_z),x3(point_x),x3(point_y),x3(point_z),x4(point_x),x4(point_y),x4(point_z),x5(point_x),x5(point_y),x5(point_z)
a1,10,12,3,35,13,3,,,,,,,15,19,9
b1,65,11,2,20,22,5,,,,,,,,,
a2,25,17,7,,,,,,,,,,,,
b2,75,17,7,,,,,,,,,,50,23,1
c1,,,,70,87,2,,,,,,,,,
c2,80,67,4,,,,,,,,,,,,
c3,,,,85,51,6,,,,,,,,,