Question

In: Computer Science

Write a python program that has: function createCustomerRecord that has 1 parameter, the filename to read...

Write a python program that has:

  1. function createCustomerRecord that has 1 parameter, the filename to read from, the method then reads all the records from the file and returns a dictionary. Each record in the file has the following format

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

  1. Write a function printCustomerRecord that has 2 parameters, a dictionary of the customer record and the output filename, the function will then print to the output file, the dictionary in the following format

CivilId: civilidnumber

Name: name

Address: address

Telephone Number: telephone

CivilId: civilidnumber

Name: name

Address: address

Telephone Number: telephone

Etc…

  1. Write a main function that will print to the screen ‘Hello’, then call function createCistomerRecord and pass it the input file name then call function printCustomerRecord and pass it the dictionary and the output file name. Then finally print to the screen ‘goodbye’

  1. Call the main function.

Solutions

Expert Solution

****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:


Related Solutions

In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given...
In Python write a function with prototype “def wordfreq(filename = "somefile.txt"):” that will read a given file that contains words separated by spaces (perhaps multiple words on a line) and will create a dictionary whose keys are the words and the value is the number of times the word appears. Convert each word to lower case before processing.
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
python fill in these questions--- 1. Examine target_map.txt. Write a Python program that will read this...
python fill in these questions--- 1. Examine target_map.txt. Write a Python program that will read this file, then compute and display the number of targets (#). Copy your Python program below: target_map.txt S##............................. ..#............................. ...######.........##########.... ........#..................#.... ........#.................#..... ........#####.......######...... ............#......#............ ............#.......#........... ............#.......#........... ......#######.......#........... ......#.............#........... ......#..............###........ ......#................#........ .......#...............#........ ........#...............#....... .........#.......#......#....... .........#..............#....... ............#...........#....... ............#...#.......#....... ............#...#........#...... ............#...#.........#..... ............#...#.........#..... ............#..........#........ ............#..............#.... ...##..#####....#..........#.... .#..............#...........#... .#..............#...........#... .#......#######............#.... .#.....#................#....... .......#................#....... ..####.........#.....#.......... ................######..........
Create a new Python program (you choose the filename) that contains a main function and another...
Create a new Python program (you choose the filename) that contains a main function and another function named change_list. Refer to the SAMPLE OUTPUT after reading the following requirements. In the main function: create an empty list. use a for loop to add 12 random integers, all ranging from 50 to 100, to the list. use second for loop to iterate over the list and display all elements on one line separated by a single space. display the 4th element,...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT