Why does my panda dataframe revert back upon exiting code?

Viewed 93

I have a code that logins into a website using a username and password stored in a CSV file. Then a password will get updated where the password column is in my CSV file, at the end of the steps.

My current CSV layout is like this:

url,usenameIndex,passwordIndex,index3,index4,index5,ID
someurl.com,3,4,username,password,,1

I then have a function that I call to update my CSV, to update passwordindex which goes and updates index4. This is my functions:

   def updateCSV(self, data_index, password_index, newPassword):
        print("Updating CSV with new password")
        self.data[data_index][password_index] = newPassword
        df = pd.DataFrame(self.data)
        df.to_csv(self.filePath, index=False)    
  def password_generator(self):
        today = date.today()
        d1 = today.strftime("%m%d%Y")
        print("d1 =", d1)
        letters="success@"+d1
        return letters

I then called updateCSV into a function of the currently logged in account and have it update the CSV.

  def myfunction(self, data,data_index): #issue is here
        #some code here
        new_password = self.password_generator()
        self.updateCSV(data_index, data[2], new_password)

The issue I am having is, my CSV file does get updated and ends up looking like this:

    0,1,2,3,4,5,6
    someurl.com,3,4,username,success@6202021,,1

but it ends up reverting back to the original CSV upon exiting the python code. I'm unsure why my CSV columns turn into numbers but my password is updated but then changes back to normal and exits. How do I fix this? I'm quite new to this and unsure whats going on. Doesn't show any errors either.

Updated coded:

    def readFromCSV(self, file):
        self.df = pd.read_csv(file) # use pandas to read the csv file
        self.urls  = self.df.url.to_numpy()
        self.data  = self.df.to_numpy()
        data_index = 0
        for url, data in zip(self.urls, self.data):
            self.processEmail(url.strip(),data, data_index) 
            data_index +=1
        #save the update CSV file
        self.df.to_csv(file, index=False)
0 Answers
Related