csv - Simplest method to edit password in text file Python -


i have following code, goes through stages of reading file in line line, converting list, editing field in list, last stage rewriting original file, edit included.

i interested in answers suggest easiest/simplest fix (without use of pandas/numpy etc) , using original code provided.

my current algorithm includes having create new file records, except 1 linked username, , writing in list it. seems arduous , unnecessary. solutions gratefully received!

the task explained more in comments:

code

 """ ==============task     allow user change or edit password     1. search given username     2. edit password field given username     3. save new updated username , password file / updated     """      import csv     def main():         #1. code snippet asks user username , allows them change password record         updatedlist=[]         open("fakefacebook.txt",newline="") f:           reader=csv.reader(f)           print("change password?!")           username=input("enter username required user:")           row in reader: #for every row in file               field in row:                      if field==username: #if field == required username                         updatedlist.append(row) #add each row, line line, list called 'udpatedlist'                         newpassword=input("enter new password")                         #print(updatedlist[0][1]) (this how @ password field, noting nested list)                         updatedlist[0][1] = newpassword #set field password new password             updatepassword(updatedlist)      def updatepassword(updatedlist):         open("fakefacebook.txt","w",newline="") f:             writer=csv.writer(f)             writer.writerows(updatedlist)             print("file has been updated")       main() 

note: @ moment code allows user change password (and changed in list). list comprised of user's record. overwrites single record (with changed password) text file, instead of required (original file contents + edit)

file contents

username,password,email,no_of_likes marvr,pass123,marv@gmail.com,400 smithc,open123,cart@gmail.com,200 blogsj,2bg123,blog@gmail.com,99 

required output

if tested with: marvr change password to: boo123

new file should contain:

username,password,email,no_of_likes marvr,**boo123**,marv@gmail.com,400 smithc,open123,cart@gmail.com,200 blogsj,2bg123,blog@gmail.com,99 

any comments/explanations best way approach teaching beginners appreciated. seems odd python hasn't developed module of sort make editing field in file easier 3 step algorithm quite arduous beginners (i'm talking 13-14 year olds) work out

@vpfb's answer should taken consideration well.

this answer updating records without replacing existing records in file. there okay, good, great, best , right ways this.

import csv  def main():     #1. code snippet asks user username , allows them change password record     updated_list = []     cached_list = []      open("fakefacebook.txt", newline="") f:         reader = list(csv.reader(f)) # convert iterable list make things easier.         print("change password?!")         username=input("enter username required user: ")         cached_list = reader # store copy of data.          row in reader: #for every row in file             field in row:                   if field == username: #if field == required username                     updated_list.append(row) #add each row, line line, list called 'udpated_list'                     newpassword = input("enter new password: ")                     #print(updatedlist[0][1]) (this how @ password field, noting nested list)                     updated_list[0][1] = newpassword #set field password new password          update_password(updated_list, cached_list)  def update_password(updated_list, cached_list):     index, row in enumerate(cached_list):         field in row:             if field == updated_list[0]:                 cached_list[index] = updated_list # replace old record updated record.      open("fakefacebook.txt","w", newline="") f:         writer=csv.writer(f)         writer.writerows(cached_list)         print("file has been updated")   main() 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -