Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将多个数组写入csv_Python_Csv - Fatal编程技术网

Python将多个数组写入csv

Python将多个数组写入csv,python,csv,Python,Csv,我有一个opencv python程序,可以正确运行并打印值。但是,当我尝试将打印的值写入csv文件时,我会遇到错误。 代码如下: for testingPath in paths.list_images(args["testing"]): # load the image and make predictions image = cv2.imread(testingPath) boxes = detector(cv2.cvtColor(image, cv2.COLOR_

我有一个opencv python程序,可以正确运行并打印值。但是,当我尝试将打印的值写入csv文件时,我会遇到错误。 代码如下:

for testingPath in paths.list_images(args["testing"]):
    # load the image and make predictions
    image = cv2.imread(testingPath)
    boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    # loop over the bounding boxes and draw them
    for b in boxes:
        (x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
        cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
        #print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes))) -----this prints all the required values without problem on the console
我试着这样做:

我在for循环开始之前添加了一个参数:

ap.add_argument("-i", "--index", required=True, help="Path to directory of output")
output = open(args["index"], "w")
并按如下方式使用循环:

for testingPath in paths.list_images(args["testing"]):
    # load the image and make predictions
    image = cv2.imread(testingPath)
    #filename = testingPath[testingPath.rfind("/") + 1:]
    boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    #print(basename(testingPath), brandname,"Number of brands detected: {}".format(len(boxes)))
    # loop over the bounding boxes and draw them
    for b in boxes:
        (x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
        cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
        #print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes)))
        dat = str([x, y, w, h, brandname, len(boxes)])
        output.write("{},{}\n".format(testingPath, "".join(dat)))
/home/mycomp/VideoExtract/28157.jpg,[83, 349, 164, 383, 'Pirelli', 1]
上述代码按如下方式打印值:

for testingPath in paths.list_images(args["testing"]):
    # load the image and make predictions
    image = cv2.imread(testingPath)
    #filename = testingPath[testingPath.rfind("/") + 1:]
    boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    #print(basename(testingPath), brandname,"Number of brands detected: {}".format(len(boxes)))
    # loop over the bounding boxes and draw them
    for b in boxes:
        (x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
        cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
        #print(basename(testingPath),"CX:"+str(x),"CY:"+str(y),"Width:"+str(w),"Height:"+str(h),brandname,"Number of brands detected: {}".format(len(boxes)))
        dat = str([x, y, w, h, brandname, len(boxes)])
        output.write("{},{}\n".format(testingPath, "".join(dat)))
/home/mycomp/VideoExtract/28157.jpg,[83, 349, 164, 383, 'Pirelli', 1]

我正试图摆脱[]括号。所需的操作是将打印的值写入csv/文本文件。

以csv格式写入数据是一项非常常见的任务-您可以使用以下方法

将输出变量设置为CSV编写器

output = csv.writer(open(args["index"], "w"))
替换最后两行

dat = str([x, y, w, h, brandname, len(boxes)])
output.write("{},{}\n".format(testingPath, "".join(dat)))
用这条线

output.writerow((testingPath, x, y, w, h, brandname, len(boxes)))

此示例代码并不打算提出改进代码质量和/或可读性的建议。