[英]I want to write in csv file and it only write the last value (I print the value and it work but in csv not)
for box in boxes:
(x, y, w, h) = [int(v) for v in box]
a = x + w
b = y + h
cv2.rectangle(im, (x, y), (a, b), (0, 255, 0), 2)
with open('employee_file.csv', mode='w', newline='') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
employee_writer.writerow(str[x,a])
My code must write all the values in x, y, b but it writes only one line, the last.我的代码必须在 x、y、b 中写入所有值,但它只写入最后一行。
You are supposed to use append mode for writing to your file in a for loop.您应该使用append模式在 for 循环中写入文件。 Otherwise, you will end up with only the last x,y,b values in your data.
否则,您最终将只得到数据中的最后一个 x,y,b 值。
with open('employee_file.csv', mode='a', newline='')
The line above will solve your issue.上面的行将解决您的问题。 Use mode 'a' .
使用模式'a' 。 You're opening the file in write mode .
您正在以写入模式打开文件。 Write mode over-writes the previous content in the file.
写入模式会覆盖文件中的先前内容。 Therefore, every time your code opens the file, it clears the data in it.
因此,每次您的代码打开文件时,都会清除其中的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.