Pass to the txt file unique rows

by: JanioBachmann, 7 years ago


I am trying to pass unique rows to a txt file after doing a web scrapping for certain values. So the txt file involves the following:
Current date                        Amount Gained
15/07/2017                                    [660]
16/07/2017                                    [-200]
17/07/2017                                      [300]

So basically what I want to do is to write a script that only allows unique rows I dont want any duplicates because values change daily. So if a user by accident runs the script two times in one day I dont want a duplicate row in my txt file because it will affect further calculations in my data analysis. So this is the function that I currently have and I will like to know what modifications should I make?

def Cost_Revenues_Difference():

    nrevenue = revenue
    dateoftoday = time.strftime('%d/%m/%Y')
    Net_Result.append(nrevenue)

    with open('Net_Result.txt', 'a') as ac:
        for x in Net_Result:
            ac.write('n' + dateoftoday + ' ' + str(Net_Result))


Cost_Revenues_Difference()



You must be logged in to post. Please login or register an account.



It seems like you will first have to read in the text file you are writing to. Once you have read the file it will likely be stored with each line as an item in a list. If so, you can check if the line you want to add is already present in the list. To do this check you could use something like

if x not in y:
    <add line to file>



-martin_heroux 7 years ago
Last edited 7 years ago

You must be logged in to post. Please login or register an account.