每次更改变量时,CSV 文件中的行都会被覆盖

时间:2021-05-14 12:01:15

标签: python csv

我对在 Python 中使用 CSV 函数比较陌生,需要您的帮助。

我有一个 python 程序,可以计算 opencv 中轮廓之间的距离以及角度,以后每次我按键盘上的 h 时,这些数据都会存储在 CSV 文件中。问题是,每次我按 h 时,较早的行都会被新行覆盖,而不是将其保存在新行中。有什么办法可以将新变量以 CSV 格式保存在新行中?

这是我的代码的一部分。整个代码很长,所以从它贴出必要的部分-



def calcDistHex(x6, y6, x5, y5, x4, y4, x3, y3, x2, y2, x, y):
    dist1 = round(dist.euclidean((x6, y6), (x5, y5)))
    dist2 = round(dist.euclidean((x5, y5), (x4, y4)))
    dist3 = round(dist.euclidean((x4, y4), (x3, y3)))
    dist4 = round(dist.euclidean((x3, y3), (x2, y2)))
    dist5 = round(dist.euclidean((x2, y2), (x, y)))
    dist6 = round(dist.euclidean((x, y), (x6, y6)))
    
    #print(dist1)
    cv2.putText(frame, str(dist1), (round(0.5 * x6 + 0.5 * x5), round(0.5 * y6 + 0.5 * y5)) , font, 0.5, (0, 0, 0), 1)
    cv2.putText(frame, str(dist2), (round(0.5 * x5 + 0.5 * x4), round(0.5 * y5 + 0.5 * y4)) , font, 0.5, (0, 0, 0), 1)
    cv2.putText(frame, str(dist3), (round(0.5 * x4 + 0.5 * x3), round(0.5 * y4 + 0.5 * y3)) , font, 0.5, (0, 0, 0), 1)
    cv2.putText(frame, str(dist4), (round(0.5 * x3 + 0.5 * x2), round(0.5 * y3 + 0.5 * y2)) , font, 0.5, (0, 0, 0), 1)
    cv2.putText(frame, str(dist5), (round(0.5 * x2 + 0.5 * x), round(0.5 * y2 + 0.5 * y)) , font, 0.5, (0, 0, 0), 1)
    cv2.putText(frame, str(dist6), (round(0.5 * x + 0.5 * x6), round(0.5 * y + 0.5 * y6)) , font, 0.5, (0, 0, 0), 1)

    pt6 = x6, y6
    pt5 = x5, y5
    pt4 = x4, y4
    pt3 = x3, y3
    pt2 = x2, y2
    pt1 = x, y
    
    m2 = gradient(pt2,pt1)
    n2 = gradient(pt2,pt3)
    if m2 is not None and n2 is not None:
        angR2 = math.atan((n2-m2)/(1+(n2*m2)))
        angD2 = math.degrees(angR2)
        if math.isnan(angD2) is False:
            cv2.putText(frame, str(round(abs(angD2))), (pt2[0]-40,pt2[1]-20), font, 1, (0, 0, 0))        
            #print(round(abs(angD2)),(pt1[0]-40,pt1[1]-20))
    
    m3 = gradient(pt3,pt2)
    n3 = gradient(pt3,pt4)
    if m3 is not None and n3 is not None:
        angR3 = math.atan((n3-m3)/(1+(n3*m3)))
        angD3 = math.degrees(angR3)
        if math.isnan(angD3) is False:
            cv2.putText(frame, str(round(abs(angD3))), (pt3[0]-40,pt3[1]-20), font, 1, (0, 0, 0))        
            #print(round(abs(angD3)),(pt1[0]-40,pt1[1]-20))
    
    m4 = gradient(pt4,pt3)
    n4 = gradient(pt4,pt5)
    if m4 is not None and n4 is not None:
        angR4 = math.atan((n4-m4)/(1+(n4*m4)))
        angD4 = math.degrees(angR4)
        if math.isnan(angD4) is False:
            cv2.putText(frame, str(round(abs(angD4))), (pt4[0]-40,pt4[1]-20), font, 1, (0, 0, 0))        
            #print(round(abs(angD4)),(pt1[0]-40,pt1[1]-20))
    
    m5 = gradient(pt5,pt4)
    n5 = gradient(pt5,pt6)
    if m5 is not None and n5 is not None:
        angR5 = math.atan((n5-m5)/(1+(n5*m5)))
        angD5 = math.degrees(angR5)
        if math.isnan(angD5) is False:
            cv2.putText(frame, str(round(abs(angD5))), (pt5[0]-40,pt5[1]-20), font, 1, (0, 0, 0))                
            #print(round(abs(angD5)),(pt1[0]-40,pt1[1]-20))
    
    m6 = gradient(pt6,pt5)
    n6 = gradient(pt6,pt1)
    if m6 is not None and n6 is not None:
        angR6 = math.atan((n6-m6)/(1+(n6*m6)))
        angD6 = math.degrees(angR6)
        if math.isnan(angD6) is False:
            cv2.putText(frame, str(round(abs(angD6))), (pt6[0]-40,pt6[1]-20), font, 1, (0, 0, 0))        
            #print(round(abs(angD6)),(pt1[0]-40,pt1[1]-20))
    
    m = gradient(pt1,pt6)
    n = gradient(pt1,pt2)
    if m is not None and n is not None:
        angR = math.atan((n-m)/(1+(n*m)))
        angD = math.degrees(angR)
        if math.isnan(angD) is False:
            cv2.putText(frame, str(round(abs(angD))), (pt1[0]-40,pt1[1]-20), font, 1, (0, 0, 0))                
            #print(round(abs(angD)),(pt1[0]-40,pt1[1]-20))
    if cv2.waitKey(1) == ord('h'):
            timestamp = int(time.time() * 10000)
            with open('dataset.csv', 'w', newline='') as dataset_file:
                dataset = csv.DictWriter(
                    dataset_file,
                    ["timestamp", "shape", "Side1", "Side2", "Side3", "Side4", "Side5", "Side6", "Perimeter", "Angle1", "Angle2", "Angle3", "Angle4", "Angle5", "Angle6", "AngleSum", "Error"]
                )
                dataset.writeheader()
                dataset.writerow({
                    "timestamp": timestamp,
                    "shape": "Hexagon",
                    "Side1": dist1,
                    "Side2": dist2,
                    "Side3": dist3,
                    "Side4": dist4,
                    "Side5": dist5,
                    "Side6": dist6,
                    "Perimeter": (dist1 + dist2 + dist3 + dist4 + dist5 + dist6),
                    "Angle1": angD,
                    "Angle2": angD2,
                    "Angle3": angD3,
                    "Angle4": angD4,
                    "Angle5": angD5,
                    "Angle6": angD6,
                    "AngleSum": (angD + angD2 + angD3 + angD4 + angD5 + angD6),
                    "Error": "To Do"

                })
                
    return dist1, dist2, dist3, dist4, dist5, dist6, angD, angD2, angD3, angD4, angD5, angD6;

这是存储文件的定义函数。

此函数稍后在另一个循环中调用 -

            if len(approx) == 6:
                for j in n:
                    if(i % 2 == 0):
                        x6 = n[i - 10]
                        y6 = n[i - 9]
                        
                        x5 = n[i - 8]
                        y5 = n[i - 7]

                        x4 = n[i - 6]
                        y4 = n[i - 5]

                        x3 = n[i - 4]
                        y3 = n[i - 3]
                        
                        x2 = n[i - 2]
                        y2 = n[i - 1]
                        
                        x = n[i]
                        y = n[i + 1]
                        
                        #print(x, y, x2, y2, x3, y3, x4, y4)
                        string = str(x) + " " + str(y)
                        cv2.circle(frame, (x, y), 2, (0,0,100), 2)                            
                        cv2.putText(frame, string, (x, y), font, 0.5, (138, 138, 54), 2)

                        calcDistHex(x6, y6, x5, y5, x4, y4, x3, y3, x2, y2, x, y)
                                
                                    # text on remaining co-ordinates.

                i = i + 1

    cv2.imshow("Frame", frame)
    cv2.imshow("Mask", threshold)

感谢任何帮助。用python编写。

1 个答案:

答案 0 :(得分:-1)

尝试更改此行:

with open('dataset.csv', 'w', newline='') as dataset_file:

作者:

with open('dataset.csv', 'a', newline='') as dataset_file:

'w' 表示覆盖。 'a' 表示追加。更多信息:https://stackoverflow.com/a/1466036/3922534

编辑: 为避免每次写入时出现重复的标题,请删除该行:

dataset.writeheader()

从您当前的代码中,并在您的程序循环之前添加以下代码。它将通过用 CSV 标头覆盖文件来初始化文件(注意 'w' 模式),然后程序中的循环将只添加数据行。

with open('dataset.csv', 'w', newline='') as dataset_file:
    dataset = csv.DictWriter(
        dataset_file,
        ["timestamp", "shape", "Side1", "Side2", "Side3", "Side4", "Side5", "Side6", "Perimeter", "Angle1", "Angle2", "Angle3", "Angle4", "Angle5", "Angle6", "AngleSum", "Error"]
    )
    dataset.writeheader()
相关问题