[英]output multiples row to csv python
python新手,所以如果这不可能对不起浪费时间..
我想在写出时将控制台中打印的完整数据集写入 csv。 不幸的是,我只得到第一行,采用所需的 xy 格式。
谢谢
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()
您可以在打印它们的同时写入值
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.