Question

In: Computer Science

In python, read the file credit_cards.txt into a dictionary with the count of how many cards...

In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file.

credit_cards.txt contains the following data:

John Smith, Discover

Helen Jones, Visa

Jerry Jones, Master Card

Julio Jones, Diners Club

Fred Jones, Diners Club

Anthony Rendon, Platinum Visa

Juan Soto, Platinum Visa

George Jones, American Express

Brandon Allen, Visa

Henry Beureguard, Visa

Allen Jackson, Master Card

Faith Hill, Platinum Visa

David Smith, Master Card

Samual Jackson, Visa

Donny Jackson, Master Card

Herb Smith, Visa

Ryan Day, American Express

Herm Edwards, American Express

Daisey Fuentes, Master Card

Henry Flood, Visa

Solutions

Expert Solution

Program Code to Copy:

file = open('credit_cards.txt', 'r')
credit_cards = {}
clean_lines = []
for i in file:
    # Filtering out lines that are empty(if line is not blank we take it into another list(clean_lines))
    if i != '\n':
        clean_lines.append(i)

file.close()
for i in clean_lines:
    # Splitting the line based on comma(,) and storing the values
    card_holder_name, card_type = i.split(',')
    # Removing whitespaces from both the ends
    card_type = card_type.strip()
    # If card is in dictionary add one
    if card_type in credit_cards.keys():
        credit_cards[card_type] += 1
    # If card is not in dictionary record this entry as first entry
    else:
        credit_cards[card_type] = 1

print('Number of Credit Cards of each type is given below\n')
for credit_card in credit_cards.keys():
    # Storing value of number of cards of each type
    no_of_cards = credit_cards[credit_card]
    # Printing a message based on number of cards of given day
    if no_of_cards > 1:
        print('There are', no_of_cards, credit_card, 'credit cards')
    else:
        print('There is 1', credit_card, 'credit card')

Code Screenshot:

Output Screenshot:


Related Solutions

Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original three-or-more...
Done in c++, Read an unsorted keywords file once to determine how many words are in...
Done in c++, Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT