I want to create a list of key-value pair with the output from /getService route.
I am able to filter the data that i wanted (Suburb and Services) from csv file vet_service_locations but want to have it as a key-value pair. Where keys are suburbs and services and values would be the relevant output.
I'm a beginner and tried different methods but nothing seems to work.
from bottle import html_quote, route, run, template, response, request
import petl as etl
from json import dumps
import csv
output = []
reading_file = etl.fromcsv('vet_service_locations.csv')
print(reading_file)
@route('/getServices')
def details():
postcode = request.query.postcode
print(postcode)
for row in reading_file:
if row[2] == postcode:
output.append(row[1])
output.append(row[4])
print(output)
run(host='localhost', port=3000, debug=True)
Vet_service_location.csv data image is in this link
Output I'm getting
[('Adelaide', 'Small_Animal'), ('Adelaide', 'Oncology'), ('Adelaide', 'Surgery'), ('Adelaide', 'Annual_Checkup'), ('Adelaide', 'Wildlife')]
Output I want
suburb, values
[('Adelaide', 'Small_Animal'),
('Adelaide', 'Oncology'),
('Adelaide', 'Surgery'),
('Adelaide', 'Annual_Checkup'),
('Adelaide', 'Wildlife')]
So, kinda like the table, the same structure in which the vet_service_locations.csv is.
CodePudding user response:
If you want output
to be a list of key-value pairs with suburb as the key and service as the value, then you should replace the lines
output.append(row[1])
output.append(row[4])
with just output.append((row[1], row[4]))
so that output
is a list of tuples.
Alternatively, you may want to make output
a dictionary. To do this, replace your declaration output = []
with output = {}
and then replace the lines
output.append(row[1])
output.append(row[4])
with output[row[1]] = row[4]
.
CodePudding user response:
In addition to @Andrew's answer, try pprint
to output the exact format as what you wanted.
import pprint
# ...
# ...
for row in reading_file:
if row[2] == postcode:
output.append((row[1], row[4]))
print("suburb, values")
pprint.pprint(output)
Output
suburb, values
[('Adelaide', 'Small_Animal'),
('Adelaide', 'Oncology'),
('Adelaide', 'Surgery'),
('Adelaide', 'Annual_Checkup'),
('Adelaide', 'Wildlife')]
Or, if you want the output to be table-like, try using formatted string literals/ f-strings
.
# ...
# ...
sub_width = val_width = 0
for row in reading_file:
if row[2] == postcode:
output.append((row[1], row[4]))
# calculate the minimum width of each column
sub_width = len(row[1]) if len(row[1]) > sub_width else sub_width
val_width = len(row[4]) if len(row[4]) > val_width else val_width
print(f" {'='*(sub_width 2)} {'='*(val_width 2)} ")
print(f"| {'Suburb':{sub_width}} | {'Service':{val_width}} |")
print(f" {'='*(sub_width 2)} {'='*(val_width 2)} ")
for row in output:
print(f"| {row[0]:{sub_width}} | {row[1]:{val_width}} |")
print(f" {'-'*(sub_width 2)} {'-'*(val_width 2)} ")
Output
========== ================
| Suburb | Service |
========== ================
| Adelaide | Small_Animal |
---------- ----------------
| Adelaide | Oncology |
---------- ----------------
| Adelaide | Surgery |
---------- ----------------
| Adelaide | Annual_Checkup |
---------- ----------------
| Adelaide | Wildlife |
---------- ----------------