Question

Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries:...

Phython

read_file (fp)  D1,D2,D3:

  1. a) This function read a file pointer and returns 3 dictionaries:
    - Parameters: file pointer (fp)
    - Returns : 3 dictionaries of list of tuples - The function displays nothing

  2. b) You must use the csv.reader because some fields have commas in them.

  3. c) Each row of the file contains the name of a video game, the platform which it was released, the release year, the genre (i.e. shooter, puzzle, rpg, fighter, etc.), the publishing

CSE 231

Spring 2020

d) e)

f)

company, and regional sales across north America, Europe, Japan, and other regions. For this project, we are only interested in the following columns:

     name = line[0]
     platform = line[1]
     year = int(line[2])
     genre = line[3]
     publisher = line[4]
     na_sales = float(line[5])
     europe_sales = float(line[6])
     japan_sales = float(line[7])
     other_sales = float(line[8])

All strings should be converted to lower case and stripped of the trailing/forward white spaces.

Multiply each regional sales column by 1,000,000. The program must compute the total global sales by adding all the regional sales (na_sales, europe_sales, japan_sales, other_sales).

This function returns 3 separate dictionaries. The first dictionary, with ‘name’ as key, will contain the data used to display the global sales per year or platform. The second dictionary, with ‘genre’ as key, contains the data used to display the regional sales per genre. The third dictionary, with ‘publisher’ as key, contains the data used to display the global sales by publisher. All of 3 dictionaries have a list of tuples as values:

  D1 = { name:[(name, platform, year, genre, publisher,
        global_sales), ...], ...}
  D2 = { genre: [(genre, year, na_sales, eur_sales,
        jpn_sales, other_sales, global_sales), ...], ...}
  D3 = {publisher: [(publisher, name, year, na_sales,
        eur_sales, jpn_sales, other_sales, global_sales),
        ...], ...}

You should ignore all the values that are not valid: - ‘year’ should be integer (int)
- All regional sales should be floats (floats).

Once the file is read and all the data is stored in the 3 dictionaries, you need to sort each dictionary alphabetically in ascending order by their keys. The values for the 3 dictionaries should also be sorted by the last element of the tuples in reverse order (global_sales).

Hint: Dictionaries are insertion sorted meaning the order of insertion into the dictionary is preserved. You should first get all the keys of a dictionary and sort

g)

CSE 231 Spring 2020

them. Then iterate through the sorted list of keys and insert their corresponding values into a new dictionary. You should sort the values before inserting them.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have used a made-up .csv file having the same structure as described above. It provided below

watch dogs ,pc,2014,fighter,companyA,10,100,1000,20000

gta 5,pc,2013,advanture,Rockstars ,10,100,1000,20000

The Witcher 3: Wild Hunt,pc,2020,opean world,companyB,10,100,1000,20000

Control,pc,2018,advanture,companyC,10,100,1000,20000

fortnite,pc,2017,battle royal,EPIC,10,100,1000,20000

pubg,pc,2016,battle royal,TENSENT,10,100,1000,20000

Code :

import csv

def sort(x):

d = {}

for i in sorted(x.keys()):

# print(i)

d[i] = x[i]

return d

def read_file(fp):

  

#reading the file

  

with open(fp) as csvfile:

spamreader = csv.reader(csvfile, delimiter=',')

file_rows = [row for row in spamreader]

# print(file_rows)

#extracting data into the specified columns

names=[]

platform=[]

year=[]

genre=[]

publisher=[]

na_sales=[]

europe_sales=[]

japan_sales=[]

other_sales=[]

for i in file_rows:

names.append(i[0].lower().strip())

platform.append(i[1].lower().strip())

year.append(int(i[2]))

genre.append(i[3].lower().strip())

publisher.append(i[4].lower().strip())

na_sales.append(float(i[5]))

europe_sales.append(float(i[6]))

japan_sales.append(float(i[7]))

other_sales.append(float(i[8]))

#calculate global

na_sales = list(map(lambda x: x * 1000000,na_sales))

  

europe_sales = list(map(lambda x: x * 1000000,europe_sales))

japan_sales = list(map(lambda x: x * 1000000,japan_sales))

other_sales = list(map(lambda x: x * 1000000,other_sales))

global_sales = [0] * len(other_sales)

for i in range(len(other_sales)):

global_sales[i] = na_sales[i] + europe_sales[i] + japan_sales[i] + other_sales[i]

# dic_1 key : names and contains info. abt. the global sales

D1 = {}

D2 = {}

D3 = {}


for i in range(len(names)):

D1[names[i]] = [(names[i],platform[i],year[i],genre[i],publisher[i],global_sales[i])]

D2[genre[i]] = [(genre[i],year[i],na_sales[i],europe_sales[i],japan_sales[i],other_sales[i],global_sales[i])]

D3[publisher[i]] = [(publisher[i],names[i],year[i],na_sales[i],europe_sales[i],japan_sales[i],other_sales[i],global_sales[i])]

  

# first sorting distionary by their keys

# print(sorted(D1.items(),reverse=True))

  

D1 = sort(D1)

  

D2 = sort(D2)

D3 = sort(D3)

return D1,D2,D3


if __name__ == "__main__":

d1,d2,d3 = read_file("game_data.csv")

Note :if you have any queries then comment below, I'm very happy to solve your doubts, thank you. ..

Add a comment
Know the answer?
Add Answer to:
Phython read_file (fp)  D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • Write an open_file() and a function that reads a csv data file and returns a dictionary...

    Write an open_file() and a function that reads a csv data file and returns a dictionary Microsoft Word - project09_final_RJE.docx 1/17 This project focuses on analyzing a publicly available dataset containing information about the spread of nCoV. Since this is an active situation, this dataset is constantly being updated with the latest figures. However, for our purposes, we will use a static version of the dataset provided to you with this project (ncov.csv). This static version was lasted updated on...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • Help needed with Python 3: Dictionaries and Sets. The problem is one that asks the user to create...

    Help needed with Python 3: Dictionaries and Sets. The problem is one that asks the user to create a completed program that prompts the user for the name of a data file, and reads the contents of that data file, then creates variables in a form suitable for computing the answer to some questions. The format should be in the same manner as the attached .py file. All subsequent assignment details (.py and .csv files) can be found at this...

  • Questions 1. How to create a comment in python? 2. The way to obtain user input...

    Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • [C++] Outline: The movie data is read from a file into an array of structs and...

    [C++] Outline: The movie data is read from a file into an array of structs and is sorted using the Selection Sort method and the search is done using the Binary Search algorithm with the year of release as key. This assignment upgrades to an array of pointers to the database elements and converts the Selection Sort and Binary search procedure on the database to selection sort and binary search on the array of pointers to the database.   Requirements: Your...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT