Python tough question

Associate
Joined
28 Oct 2014
Posts
472
Hi

I got this question to do

The authorities have a file of known vehicle registrations and the vehicle’s owner. Those vehicles with standard registrations can be looked up in this file and a fine automatically sent out. A new file is created by comparing the information from the average speed recording system with the file of registered vehicles and their owners’ details. This new file should include the owner’s name and address details, the registration of the vehicle and the average speed of the vehicle in the section of road. Create a program for creating a file of details for vehicles exceeding the speed limit set for a section of road. You will need to create a suitable file with test data, including standard registrations and vehicle owner information.

However I am not sure how to do it. I know that I could use databases but I think that would be too much work so I was thinking of maybe using csv files. But I don't know how to use them. So if you could give me some help it would be great.
 
Associate
Joined
29 May 2006
Posts
2,276
Location
Here ya go!

Code:
import csv

def fine_naughty_people(road_section, naughty_ppl_file_path,
                        known_vehicle_file_path, output_file_name):

    with open(naughty_ppl_file_path, 'rb') as np_file_obj, \
         open(known_vehicle_file_path, 'rb') as kv_file_obj, \
         open(output_file_name, 'wb') as output_file_obj:

        naughty_ppl = csv.DictReader(np_file_obj)
        known_vehicles = list(csv.DictReader(kv_file_obj))

        fine_fields = ['Name', 'Address', 'Avg Speed']
        output_file = csv.DictWriter(output_file_obj, fine_fields)
        output_file.writeheader()

        for np in naughty_ppl:
            for kv in known_vehicles:
                if np['car_reg'] == kv['car_reg'] \
                    and np['road_section'] == road_section:
                    data = {
                        'Name': kv['name'],
                        'Address': kv['address'],
                        'Avg Speed': np['avg_speed'],
                    }
                    output_file.writerow(data)

fine_naughty_people('A', 'naughty_ppl.csv', 'known_vehicles.csv', 'fine_file.csv')[CODE]
[/CODE]

Heres our naughty_ppl.csv:

Code:
car_reg	road_section	avg_speed
1234	A	55
1235	B	55
1236	A	55

Here's our known_vehicles.csv:

Code:
name	car_reg	address
Joe Regan	1234	15 Council Estate, London
Kaylee Smith	1235	4 Chav Crescent, Britwell
Jordan Dunn	1236	1A Highrise, Smackden

Here's all the scumbag criminals and dossers that will receive a fine.csv:

Code:
Name	Address	Avg Speed
Joe Regan	15 Council Estate, London	55
Jordan Dunn	1A Highrise, Smackden	55
 
Associate
Joined
29 May 2006
Posts
2,276
Location
I kinda get what you're saying but he said he had no idea about CSV's. Now he can see how you can work with 3 file objects within 1 context manager, which is not apparently obvious from the documentation!
if you look at the csv module docs in isolation.
 
Associate
Joined
10 Nov 2013
Posts
1,808
There's a bit of a difference between giving someone a full solution and providing a starting point so that they can find the right solution themselves.
 
Back
Top Bottom