简体   繁体   English

如何检查 csv 文件中是否已经存在这样的行

[英]How to check if there is already such a line in a csv file

I have a csv log file of the signal that records the values of this signal itself.我有一个记录该信号本身值的信号的 csv 日志文件。

The values are duplicated in it.值在其中重复。 What I want to avoid.我想避免的。

I'm trying something like this我正在尝试这样的事情

#There's a function torn out of the class right away
def Signal(self):
        df = pd.read_csv('Datasets\\RobotMath\\Answer-Step-Score.csv', usecols=['Time','Answers','step','step2','Score' ])
        df = df.tail(1)
        if df.step.item() != 0:
            self.value = df.step.item()
        elif df.Answers.item() != 0:
            self.value = df.Answers.item()
        elif df.step2.item() != 0:
            self.value = df.step2.item()
        self.b = pd.DataFrame({(self.value, df.Time.item(), df.Score.item())})
        dfa = pd.read_csv('Datasets\\RobotMath\\Signal-log.csv', usecols=['Time'])
        self.dfa = dfa.tail(1)
        if df.Время.item() != dfa.Time.tolist():
            self.b.to_csv('Datasets\\RobotMath\\Signal.csv',index=False, header=['Sig', 'Time', 'Step'])
            self.b.to_csv('Datasets\\RobotMath\\Signal-log.csv',index=False,mode='a', header=False)
        else:
            print('The signal has not changed')

The log file is created in another function once when the loop is started and records one second.日志文件在循环启动时在另一个 function 中创建一次并记录一秒。

Further, according to my logic, I assign a signal value to a variable and compare this signal with a log file, and if there is no such signal in the log file, then write it to the log and send it to the signal.进一步,按照我的逻辑,我给一个变量赋值一个信号值,并将这个信号与一个日志文件进行比较,如果日志文件中没有这个信号,则将其写入日志并发送给信号。

But for some reason I have a duplication of the signal in the logs但由于某种原因,我在日志中有重复的信号

I tried to do the 'if' check in different ways我尝试以不同的方式进行“if”检查

if df.Time.item().equals(dfa.Time.item()):
            self.b.to_csv('Datasets\\RobotMath\\Signal.csv',index=False, header=['Sig', 'Time', 'Step'])
            self.b.to_csv('Datasets\\RobotMath\\Signal-log.csv',index=False,mode='a', header=False)
        else:
            print('The signal has not changed')

But it didn't work that way但它没有那样工作

And it didn't work when I did so当我这样做时它没有用

if df.Время.item() in dfa['Time']:
            self.b.to_csv('Datasets\\RobotMath\\Signal.csv',index=False, header=False)
            self.b.to_csv('Datasets\\RobotMath\\Signal-log.csv',index=False,mode='a', header=False)
        else:
            print('The signal has not changed')

How do I do this check and not write the same values?如何进行此检查而不写入相同的值?

Would using df.drop_duplicates solve this issue?使用df.drop_duplicates会解决这个问题吗?

self.b = pd.DataFrame({
    (self.value, df.Time.item(), df.Score.item())
}).drop_duplicates(subset='Signal')

Could you maybe provide example data for this so that the issue can be replicated?您能否为此提供示例数据,以便可以复制该问题?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM