CSV编写器问题

时间:2016-12-09 19:48:22

标签: python export-to-csv

def little_boxes(new_frame, y_pos, box_size):
""" Arrange boxes across new frame and return a list of boolean values represting if a line is present in each box"""
    if not isinstance(new_frame, numpy.ndarray):
       raise ValueError("A valid numpy array must be passed in")
    width, height = new_frame.shape[:2]  # height, width -> y, x
    num_boxes = height / box_size[0]
    box_list = []
    for box_index in range(num_boxes):
        # Check for laser in each box
        box_x1 = box_index * box_size[1]
        box_x2 = (box_index + 1) * box_size[1]
        box_y = y_pos
        cropped_image = new_frame[box_y: box_y + box_size[1], box_x1: box_x2]
        box_list.append(laser_check(cropped_image))
    print(box_list)

    with open("output.csv","wb") as f:
        writer = csv.writer(f)
        writer.writerow((box_list))
    print ('output.csv')
    f.close()

以上是我用来从网络摄像头收集数据然后将数据存储到excel文件中的代码的一部分。但是它只保存最后一行。例如,如果我得到这个列表:

[1,0,0,0]

[0,1,1,1]

[0,0,1,1]

excel文件中保存的唯一内容是[0,0,1,1]

有关如何解决此问题的任何想法?我还希望能够为每一行加盖时间戳。

1 个答案:

答案 0 :(得分:0)

假设box_list是列表清单:

import csv
box_list = [[1,0,0,0], [0,1,1,1], [0,0,1,1]]

with open("output.csv","wb") as f:
    writer = csv.writer(f)
    for row in box_list:
        writer.writerow(row)
# no need to close f