Answer:
Code
import csv
def open_file():
try:
fname=input("Enter the filename to open (xxx.yyy): ")
fp=open(fname,encoding='utf-8')
return fp
except FileNotFoundError:
print("File not found! Please Enter correct filename with extension")
return open_file()
def read_file(fp):
fields = []
rows = []
csvreader = list(csv.reader(fp))
# skiped 1 row because it contains field name
fields = csvreader[0]
D1={}
D2={}
D3={}
# accessing the data of csv file
for line in csvreader[1:]:
name = line[0].lower().strip()
platform = line[1].lower().strip()
if line[2]!='N/A':
year = int(line[2])
else:
year=line[2]
genre = line[3].lower().strip()
publisher = line[4].lower().strip()
na_sales = float(line[5])
eur_sales = float(line[6])
jpn_sales = float(line[7])
other_sales = float(line[8])
global_sales=(na_sales+eur_sales+jpn_sales+other_sales)*1000000
if name not in D1:
D1[name]=[]
D1[name].append((name, platform, year, genre, publisher,global_sales))
if genre not in D2:
D2[genre]=[]
D2[genre].append((genre, year, na_sales, eur_sales,jpn_sales, other_sales, global_sales))
if publisher not in D3:
D3[publisher]=[]
D3[publisher].append((publisher, name, year, na_sales,eur_sales, jpn_sales, other_sales, global_sales))
# sorting
temp={}
# sorting keys
for Keys in sorted (D1) :
ls=D1[Keys]
ls=sorted(ls,key=lambda x: x[-1]) # sorting values
temp[Keys]=ls
D1=temp.copy()
temp={}
# sorting keys
for Keys in sorted (D2) :
ls=D2[Keys]
ls=sorted(ls,key=lambda x: x[-1]) # sorting values
temp[Keys]=ls
D2=temp.copy()
temp={}
# sorting keys
for Keys in sorted (D3) :
ls=D3[Keys]
ls=sorted(ls,key=lambda x: x[-1]) # sorting values
temp[Keys]=ls
D3=temp.copy()
return D1,D2,D3
def main():
fp=open_file()
D1,D2,D3=read_file(fp)
# displaying the row_count in dictionaries
cnt=0
for d in D1:
cnt+=1
print(cnt)
print("\n\n=============================================\n\n")
cnt=0
for d in D2:
cnt+=1
print(cnt)
print("\n\n=============================================\n\n")
cnt=0
for d in D3:
cnt+=1
print(cnt)
print("\n\n=============================================\n\n")
if __name__=="__main__":
main()
Explanation:
see code and see output attached