In: Computer Science
Write a python program that has:
CivilIdNumber
Name
Telephone#
Address
CivilIdNumber
Name
Telephone#
Address
CivilIdNumber
Name
Telephone#
Address
Etc
A record always consists of 4 lines (civilid, name, telephone and address). You can find a sample input file on last page of this assignment, copy it and past it into a .txt file and save it to the same directory that your python program resides on.
The function then creates a dictionary with the civilIdNuber as the key and the rest of the record as a list with index 0 being the name, index 1 being the telephone# and index 2 being Address.
When finishing reading all records the function then returns the dictionary
CivilId: civilidnumber
Name: name
Address: address
Telephone Number: telephone
CivilId: civilidnumber
Name: name
Address: address
Telephone Number: telephone
Etc…
****This require some effort so please drop a like if you are satisfied with the solution****
I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference...
Code:
import re def createCustomerRecord(filename): f = open(filename) content = f.read() content = re.split("\\n", content) content.pop() customerRecords = {} record = [] for i in range(len(content)): if i % 4 == 0: key = content[i] record = [] else: record.append(content[i]) customerRecords[key] = record return customerRecords def printCustomerRecord(dict, filename): f = open(filename, "w+") for key in dict.keys(): f.write("CivilId: " + key + "\n") f.write("Name: " + dict[key][0] + "\n") f.write("Address: " + dict[key][2] + "\n") f.write("Telephone Number:" + dict[key][1] + "\n") def main(): print("Hello") printCustomerRecord(createCustomerRecord("input.txt"), "Output.txt") print("goodbye") if __name__ == "__main__": main()
Output Screenshot:
Input File:
Output File:
Code Screenshot: