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...
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...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive. In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4]. Using the function intToList, implement the function digitsCount(a, b) that returns a list...
PYTHON: Using a counter initialized to 0, count how many even numbers are multiples of 3...
PYTHON: Using a counter initialized to 0, count how many even numbers are multiples of 3 in a range of numbers 1 to 300. If the counter reaches 20, break out of the loop and stop counting.
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...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while loop to take in user's name and desired destination for as long as there are user inputs. Prompt user to input yes to continue and no to quit. Prompt for user's name. Receive the name into the program and save it as the value of name variable. Prompt user for their desired vacation destination. Receive response and save it as the value of a...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file ( Use any for example, You can use small dataset) You need to use pandas library You need to use Flask Make search table like YouTube has.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT