Skip to content
Advertisement

How to use a CSV file and use the CSV file to have an input from a user?

I have dataset about car accidents statistics by using a .csv file. I want the user to type in State and all of the information about that State gets displayed for the user to see. How can do that?

Dataset:

State,Population,Vehicle Miles traveled (millions),Fatal Crashes,Deaths
Alabama,"4,903,185","71,735",856,930
Alaska,"731,545","5,881",62,67
Arizona,"7,278,717","70,281",910,981
Arkansas,"3,017,804","37,099",467,505
California,"39,512,223","340,836","3,316","3,606"
Colorado,"5,758,736","54,634",544,596
Connecticut,"3,565,287","31,601",233,249
Delaware,"973,764","10,245",122,132
District of Columbia,"705,749","3,756",22,23
Florida,"21,477,737","226,514","2,950","3,183"
Georgia,"10,617,423","133,128","1,377","1,491"
Hawaii,"1,415,872","11,024",102,108
Idaho,"1,787,065","18,058",201,224
Illinois,"12,671,821","107,525",938,"1,009"
Indiana,"6,732,219","82,719",751,809
Iowa,"3,155,070","33,537",313,336
Kansas,"2,913,314","31,843",362,411
Kentucky,"4,467,673","49,410",667,732
Louisiana,"4,648,794","51,360",681,727
Maine,"1,344,212","14,871",143,157
Maryland,"6,045,680","60,216",484,521
Massachusetts,"6,892,503","64,890",321,334
Michigan,"9,986,857","102,174",902,985
Minnesota,"5,639,632","60,731",333,364
Mississippi,"2,976,149","41,091",581,643
Missouri,"6,137,428","79,168",818,880
Montana,"1,068,778","12,892",166,184
Nebraska,"1,934,408","21,242",212,248
Nevada,"3,080,156","28,794",285,304
New Hampshire,"1,359,711","13,828",90,101
New Jersey,"8,882,190","78,205",525,559
New Mexico,"2,096,829","27,772",368,424
New York,"19,453,561","123,986",876,931
North Carolina,"10,488,084","122,475","1,284","1,373"
North Dakota,"762,062","9,826",91,100
Ohio,"11,689,100","114,694","1,039","1,153"
Oklahoma,"3,956,971","44,648",584,640
Oregon,"4,217,737","35,808",451,489
Pennsylvania,"12,801,989","102,864",990,"1,059"
Rhode Island,"1,059,361","7,581",53,57
South Carolina,"5,148,714","57,939",922,"1,001"
South Dakota,"884,659","9,922",88,102
Tennessee,"6,829,174","82,892","1,040","1,135"
Texas,"28,995,881","288,227","3,294","3,615"
Utah,"3,205,958","32,911",225,248
Vermont,"623,989","7,346",44,47
Virginia,"8,535,519","85,432",774,831
Washington,"7,614,893","62,530",494,519
West Virginia,"1,792,147","19,077",247,260
Wisconsin,"5,822,434","66,348",526,566
Wyoming,"578,759","10,208",120,147
U.S. total,"328,239,523","3,261,774","33,244","36,096"

I’m thinking of something like this:

crashes = 0
stateInput = input("Please enter a State: ")

print("The total of number of crashes in" , stateInput , "is:" , crashes)

Advertisement

Answer

I suggest you use the standard-library’s csv module to read the file. Specifically the csv.DictReader class which will convert each row of the CSV file into a Python dictionary which will make processing it easier.

The following code shows how to do that and also stores all the state dictionaries into a higher level stateDB dictionary so that the name of each state is associated with information about it, effectively making it a “database”. Note that the state names are converted to all uppercase to simplify look-ups.

import csv
from pprint import pprint

accidents_filepath = 'car_accidents.csv'

stateDB = dict()
with open(accidents_filepath, 'r', newline='') as csv_file:
    reader = csv.DictReader(csv_file)
    fieldnames = [name for name in reader.fieldnames if name != 'State']
    for state_info in reader:
        state = state_info['State']
        stateDB[state.upper()] = {key: state_info[key] for key in fieldnames}

#which_state = input("Please enter a state: ")
which_state = 'Mississippi'  # Hardcode for testing purposes.
print(f'Info for the state of {which_state}:')
pprint(stateDB[which_state.upper()])

Output:

Info for the state of Mississippi:
{'Deaths': '643',
 'Fatal Crashes': '581',
 'Population': '2,976,149',
 'Vehicle Miles traveled (millions)': '41,091'}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement