首页 > 解决方案 > 为什么我的熊猫数据框会在退出代码后恢复?

问题描述

我有一个使用 CSV 文件中存储的用户名和密码登录网站的代码。然后,在步骤结束时,密码列在我的 CSV 文件中的位置会更新密码。

我当前的 CSV 布局是这样的:

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

然后我有一个函数,我调用它来更新我的 CSV,更新 passwordindex 并更新 index4。这是我的功能:

   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

然后我将 updateCSV 调用到当前登录帐户的函数中,并让它更新 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)

我遇到的问题是,我的 CSV 文件确实得到了更新,最终看起来像这样:

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

但它最终会在退出 python 代码时恢复为原始 CSV。我不确定为什么我的 CSV 列变成数字但我的密码已更新但随后又恢复正常并退出。我该如何解决?我对此很陌生,不确定发生了什么。也不显示任何错误。

更新编码:

    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)

标签: pythonpandascsv

解决方案


推荐阅读