Home > OS >  Function to count number of instances in CSV file using DictReader loop
Function to count number of instances in CSV file using DictReader loop

Time:07-31

I am working on this function for my course. It is a requirement that I create a function that counts the number of cameras in each location. I am a novice, I don't understand why the loop doesn't continue through the entire CSV file. See output.

import csv

def nr_cameras(location_data):
    qty=0
    cameras={}

    with open(location_data, 'r') as p:
        csv_reader = csv.DictReader (p, delimiter = ';')
        for row in csv_reader:
            cameras[row['Kommun']] = cameras.get(row['Kommun'], 0)   1

    for k in cameras.keys():
        return (f"{k}           {cameras [k]}")


location_data = nr_cameras('locationData.csv')
print('Kommun       Nr of cameras')
print('--------------------------')
print(location_data)

Current output:

Kommun       Nr of cameras
--------------------------
Vänersborg           3

Goal output:

Kommun      Nr of cameras
---------------------------
Alingsås          17
Bengtsfors         4
Essunga           10
Falköping         28
 ...
Vara               7
Vänersborg        20
Vårgårda          13
---------------------------
There is a total of 297 cameras.

The csv file looks something like this:

MätplatsID;Namn;Vägnummer;Kommun
14002010;Bhpl Gestadvägen;E45;Vänersborg
14002080;Bhpl Asmundebyn;E45;Vänersborg
14002120;Östebyn Söder;E45;Vänersborg
14005010;VVIS station;161;Uddevalla
14005020;Nybygget V;161;Uddevalla
14006010;Bälinge nö;1900;Alingsås
14006030;Torp;1900;Vårgårda
14006040;Hols skola;1900;Vårgårda

CodePudding user response:

In your loop over the camera entries, you are returning the first entry straight away. You probably want to append to a string in this loop, and return that afterwards.

CodePudding user response:

Simply change return to print and then return the dictionary at the end. In Python, as soon as a method returns a value, method completes its call. Hence, avoid using return in a loop.

def nr_cameras(location_data):
    cameras = {}

    with open(location_data, 'r') as p:
        csv_reader = csv.DictReader(p, delimiter = ';')
        for row in csv_reader:
            cameras[row['Kommun']] = cameras.get(row['Kommun'], 0)   1

    for k in cameras.keys():
        print(f"{k}           {cameras [k]}")
   
    return cameras

print('Kommun       Nr of cameras')
print('--------------------------')
location_data = nr_cameras('locationData.csv')

Alternatively, handle all printing outside the method:

def nr_cameras(location_data):
    cameras = {}

    with open(location_data, 'r') as p:
        csv_reader = csv.DictReader(p, delimiter = ';')
        for row in csv_reader:
            cameras[row['Kommun']] = cameras.get(row['Kommun'], 0)   1

    return cameras


location_data = nr_cameras('locationData.csv')

print('Kommun       Nr of cameras')
print('--------------------------')
for k,v in location_data.items():
    print(f"{k}           {v}")
  • Related