Python将多个数组写入csv
我有一个opencv 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_
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)))
此示例代码并不打算提出改进代码质量和/或可读性的建议。