Question

write a function: Names(path): that reads from a file at location path and returns a dictionary...

write a function:

Names(path):

that reads from a file at location path and returns a dictionary mapping from column names to lists containing the data in those columns. The format of the file is a csv file. The first line is a comma separated set of string names for the columns contained in the file. The function will accumulate a dictionary mapping from year to the list of years, from name to the list of names (in the same order), and from count to the list of counts (in the same order).Convert year and count to integers.

Python

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

#required method
def Names(path):
    #opening file in read mode, assuming file exists, else this will result
    #in an exception
    file=open(path,'r')
    #creating an empty dict
    result=dict()
    #reading first line and splitting by comma to get a list of column headings
    headers=file.readline().strip().split(',')
    #looping through remaining file
    for line in file:
        #splitting line by comma to get a list of values
        fields=line.strip().split(',')
        #ensuring that there is enough number of values
        if len(headers)==len(fields):
            #looping through each index from 0 to len(headers)-1
            for i in range(len(headers)):
                #getting element at index i as value
                value=fields[i]
                #if value is numeric (year or count), converting it to integer
                if value.strip().isnumeric():
                    value=int(value)
                #if current heading is not a key in result dict, adding to dict with
                #key=headers[i] and value = empty list
                if headers[i] not in result:
                    result[headers[i]]=[]
                #appending current value to the list associated with key=headers[i]
                result[headers[i]].append(value)
    #closing file
    file.close()
    #returning the result
    return result
Add a comment
Know the answer?
Add Answer to:
write a function: Names(path): that reads from a file at location path and returns a dictionary...
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
  • 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...

  • Write a class named SatData that reads a JSON file containing data on 2010 SAT results...

    Write a class named SatData that reads a JSON file containing data on 2010 SAT results for New York City and writes the data to a text file in CSV (comma-separated values) format. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named save_as_csv that takes as a parameter a list of DBNs (district...

  • get names of input and output files from command line (NOT from user input)

     2.12 LAB 2.3: File I/O - CSV update This program shouldget names of input and output files from command line (NOT from user input)read in integers from a csv (comma-separated values) file into a vectorcompute the integer average of all of the valuesconvert each value in the vector to the difference between the original value and the averagewrite the new values into a csv file

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • Using C# or Python, write a code that reads a Microsoft Excel CSV file named "LabAssignment"...

    Using C# or Python, write a code that reads a Microsoft Excel CSV file named "LabAssignment" then outputs all the collected and formated data as another Microsoft Excel CSV file named "labOutput" The CSV file contains 25 colums as well close to 200 rows of data. Write a code in C# or Python so that: It removes all unwanted columns along with its data...the columns that should be kept have the following header: "User" "Location" "Feature" "HoursUsed" Remove all files...

  • Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...

    Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...

  • Write a program IN PYTHON that checks the spelling of all words in a file. It...

    Write a program IN PYTHON that checks the spelling of all words in a file. It should read each word of a file and check whether it is contained in a word list. A word list available below, called words.txt. The program should print out all words that it cannot find in the word list. Requirements Your program should implement the follow functions: main() The main function should prompt the user for a path to the dictionary file and a...

  • python, please!!! comma separated values to store the data. The hame_count.csv file has one header row,...

    python, please!!! comma separated values to store the data. The hame_count.csv file has one header row, of the form: name, count Each successive row then has a name and a count for how many times that name appears in a database of names that the lab made up. For this first part of the lab, you must calculate the average name count. The steps to do so are as follows: Open the file, name_count.csv • Read all the lines from...

  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • Write a function named "csv_to_list" that takes a string as a parameter representing the name of...

    Write a function named "csv_to_list" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format "<int>,<int>,<int>,<int>,<int>" and returns a new list containing all the values in the third column as integers in the same order they appear in the input file //JAVASCRIPT //

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