简体   繁体   English

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

[英]Line in CSV file gets over written each time variable is changed

I'm comparitively new to using CSV functions in python and need your help.我对在 python 中使用 CSV 函数比较陌生,需要您的帮助。

I have a python program that calculates distance between contours in opencv as well as angle, and this data is later stored in CSV file each time I press h on keyboard.我有一个 python 程序,它计算 opencv 中轮廓之间的距离以及角度,然后每次我在键盘上按h时,这些数据都会存储在 CSV 文件中。 The issue is that, each time I press h , the earlier line gets overwritten by the new line instead of saving it in new line.问题是,每次我按h ,较早的行都会被新行覆盖,而不是保存在新行中。 Is there any way I can save the new variable in new line in CSV format?有什么办法可以将新变量保存在 CSV 格式的新行中?

Here's part of my code.这是我的代码的一部分。 The whole code is long, so posting necessary part from it-整个代码很长,因此发布其中的必要部分-



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;

This is the defined function which stores the file.这是存储文件的定义的 function。

This function is later called in another loop -这个 function 后来在另一个循环中被调用 -

            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)

Any help is appreciated.任何帮助表示赞赏。 Written in python.写在 python 中。

Try changing this line:尝试更改此行:

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

By:经过:

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

The 'w' means overwrite. 'w' 表示覆盖。 The 'a' means append. “a”表示 append。 More info here: https://stackoverflow.com/a/1466036/3922534更多信息在这里: https://stackoverflow.com/a/1466036/3922534

Edit: to avoid the duplicated header at each write, delete the line:编辑:为避免在每次写入时重复 header,请删除以下行:

dataset.writeheader()

from your current code, and add the following code before your program's loop.从您当前的代码中,并在您的程序循环之前添加以下代码。 It will itinialize the file by overriding it with the CSV header (notice the 'w' mode), then the loop inside your program will only add the rows of data.它将通过用 CSV header 覆盖文件来初始化文件(注意“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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 stream 读取 CSV 文件并在写入时处理每一行? - How to read a CSV file from a stream and process each line as it is written? 当我写入 csv 文件时,数据用逗号分隔 python 中的每个字符写入 - When i Write to csv file, the data gets written with commas separating each character in python 在变量被更改的行停止 - Stop at the line where a variable gets changed 如何读取CSV或文本文件的行,循环遍历每行并保存为每行读取的新文件 - How To Read Lines of CSV or Text File, Loop Over Each Line and Save To a New File For Each Line Read 变量无意写入 - Variable gets written with no intention CSV 用Python写的文件每行之间有空行 - CSV file written with Python has blank lines between each row 如何遍历 .csv 文件的行并将每一行传递给时间序列分析模型? - How to iterate over rows of .csv file and pass each row to a time-series analysis model? 第一次写入CSV文件后跳过标题(Python) - Skip header after the first time CSV file is written into (Python) 每次运行脚本时,如何在同一.csv文件中写入新行? - How to write a new line in same .csv file each time script is ran? 检查变量在一段时间内变化了多少 - Check how much a variable has changed over an amount of time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM