Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with Python 3 Function: I can\'t seem to get this function to work. Here is

ID: 3819508 • Letter: H

Question

Help with Python 3 Function:

I can't seem to get this function to work. Here is the function:

Here is an example of an info file if it helps:

And here is the code I have that DOES NOT work properly:

def read_info_file(filename):
   info_db = {}
   with open(filename, 'r') as f:
       header = f.readline().strip().split(",")
       for line in reader(f):
           info_db[int(line[0])] = (line[1], None if not line[2] else line[2], None if not line[3] else line[3], int(line[4]), True if line[2] == "TRUE" else False)
return info_db

I just need a simple solution; nothing overly complicated. Thank you!

read info file(filename): This is only one of two functions that deals with reading a file. It accepts the file name as a string, assume it is a csv file in the format described above for an "info" file. The function needs to the file, read all the described pokemon, and create a dictionary of pokemon in the FORMAT. It returns the dictionary it creates. Note: the first line of the file is always a "header" line which does not corresponds to any pokemon; the last line of the file always ends with a newline ('In Special case: Name field in the input file might contain one comma as part of the string, for example, "Tornadus, (Incarnate Form)". You can assume the name can have at most one comma and all other fields do not have any comma.

Explanation / Answer

import csv
def readCustomCSV():
    my_dict={}
    with open("C:\Users\aditya\Documents\abc.csv","r") as f:
        reader = csv.DictReader(f, delimiter=',',quotechar ='"')
        rownum = 0;
        for row in reader:
            my_dict[row['ID']] = list(row)
        print(my_dict)
    return my_dict
if __name__ == "__main__":
    readCustomCSV()

      

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote