'output multiples row to csv python

New to python, so if this impossible sorry for time wasting..

I would like to get the full data set that is printed in console into to the csv when written out. unfortunately i am only getting the first row, in the desired x y format.

thanks

from skimage.measure import approximate_polygon, find_contours

import matplotlib.pyplot as plt
%matplotlib inline

from skimage.io import imread, imshow

import cv2

import csv 

img = cv2.imread('image.png', 0)
contours = find_contours(img, 0)

result_contour = np.zeros(img.shape + (3, ), np.uint8)
result_polygon1 = np.zeros(img.shape + (3, ), np.uint8)
result_polygon2 = np.zeros(img.shape + (3, ), np.uint8)

for contour in contours:
    # print('Contour shape:', contour.shape)

    # reduce the number of lines by approximating polygons
    polygon1 = approximate_polygon(contour, tolerance=2.5)
    print('', polygon1.shape)

file = open('test.csv', 'w')
writer = csv.writer(file)
data = [polygon1.shape]
writer.writerows(data)
file.close()


Solution 1:[1]

you could just write the values at the same time you print them

file = open('test.csv', 'a') # use 'a' instead of 'w' to append
writer = csv.writer(file)
for contour in contours:
    # print('Contour shape:', contour.shape)

    # reduce the number of lines by approximating polygons
    polygon1 = approximate_polygon(contour, tolerance=2.5)
    print('', polygon1.shape)
    writer.writerow(polygon1.shape)

file.close()

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 vstack17