Question

In: Computer Science

Python coding: Write code for each method listed. Input Text File Format: username|Firstname|Lastname|password|accountnumber|balance jh123|Jane|Hudson|45678910|AB45|900 ah444|Allie|Hun|ah1234|HHYZ|1500 def...

Python coding: Write code for each method listed.

Input Text File Format:

username|Firstname|Lastname|password|accountnumber|balance
jh123|Jane|Hudson|45678910|AB45|900

ah444|Allie|Hun|ah1234|HHYZ|1500

def build_dict():
'''
Returns a dictionary created from input file
where key is an existing username and
value is a list of fistname, lastname, account number and balance.
'''

  

def write_to_file(users):
'''
Writes the updated user information to user list file
'''

Solutions

Expert Solution

#Python code

def build_dict():
'''
Returns a dictionary created from input file
where key is an existing username and
value is a list of fistname, lastname, account number and balance.
'''

try:

dict = {}
# open file
file = open("input.txt", 'r')
for i in file:
tokens = i.rstrip('\n').split("|")
username = tokens[0]
tokens.remove(tokens[0])
dict[username] = tokens
file.close()
return dict

except FileNotFoundError as ex:
print("File not found....")


def write_to_file(users):
'''
Writes the updated user information to user list file
'''

try:
# open file to write data
outfile = open("input.txt", 'w')
for key, values in users.items():
line = ""
line += key + "|"
for i in values:
line += i + '|'
outfile.write(line[:-1]+'\n')
outfile.close()
except FileNotFoundError:
print("File not Found.....")

# test data
users = build_dict()
#update some information
for key, values in users.items():
print("update information to: "+key)
firstName = input("Update first name: ")
values[0] = firstName
lastName = input("Update last Name: ")
values[1] = lastName
password = input("Update password: ")
values[2] = password
accountnumber = input("Update account number: ")
values[3] = accountnumber
balance = input("Update balance: ")
values[4] = balance
print("Write updates to file.....")
write_to_file(users)

#screenshots for indentation help

#Output

//File(input.txt) before update

//File(input.txt) after update

//If you need any help regarding this solution...... please leave a comment...... thanks


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT