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 and value = state name. Return a dictionary at the end of the function.
d = { "AL" : "Alabama", "AK": "Alaska", ... }
Sample code:
filename = input('Enter state code file name: ') stateDic = loadStateDict(filename )
I have this so far but I it results in the error below.
def loadStateDict(filename):
d = {}
import csv
with open(filename, 'rt') as file:
reader=csv.reader(file)
for line in reader:
states = line.strip().split(',')
if len(states[1]) != 0:
d.update({states[1]:states[0]})
return
filename = input('Enter state code file name: ')
stateDic = loadStateDict(filename )
print(stateDic)
Dict
for line in reader:
File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py",
line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in
position 56: ordinal not in range(128)
def loadStateDict(filename): d = {} with open(filename, 'r') as f: for line in f: words = line.strip().split(',') if len(words[1]) != 0: d[words[1]] = words[0] return d filename = input('Enter state code file name: ') stateDic = loadStateDict(filename) print(stateDic)
Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...
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 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),...
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...
pythin: Write a function named "computed_column" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format ",,,," and writes a file named "blank.csv" containing all the data from the input file but with a sixth column containing the sum of the values from the second and fourth columns
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 //
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /** Class to read and process the contents of a standard CSV file */ public class CSVReader {...
C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything. Your function should be named...