简体   繁体   中英

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.

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.

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 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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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