I'm learning Python. I have a CSV file with these rows. I am trying to search and return rows that have year_ceremony
matched with the year
parameter the function accepts.
year_film,year_ceremony,ceremony,category,name,film,winner
1927,1928,1,ACTOR,Richard Barthelmess,The Noose,False
1927,1928,1,ACTOR,Emil Jannings,The Last Command,True
1927,1928,1,ACTRESS,Louise Dresser,A Ship Comes In,False
1928,1929,2,CINEMATOGRAPHY,Ernest Palmer,Four Devils;,False
1928,1929,2,CINEMATOGRAPHY,John Seitz,The Divine Lady,False
1928,1929,2,DIRECTING,Lionel Barrymore,Madame X,False
1928,1929,2,DIRECTING,Harry Beaumont,The Broadway Melody,False
def get_academy_awards_nominees(year):
response = []
csv_file = csv.reader(open("csvs/the_oscar_award.csv", "r"), delimiter=",")
for row in csv_file:
if row[1] == year:
response.append(row)
return response
I'm looking for a way to format matching row with the header (year_film,year_ceremony,ceremony,category,name,film,winner) as key and value and return them as JSON.
CodePudding user response:
Can't edit my previous reply !!
To convert the dict to json use
json.dumps(s)
CodePudding user response:
You can use DictReader
from csv
module for reading. It maps each row to a dict
. And use json.dumps
for converting the result(which is a list of dictionaries) to JSON format string:
import csv
import json
def get_academy_awards_nominees(year):
result = []
with open("csvs/the_oscar_award.csv") as f:
dict_reader = csv.DictReader(f)
for row in dict_reader:
if int(row["year_ceremony"]) == year:
result.append(row)
return json.dumps(result)
print(get_academy_awards_nominees(1928))
Another solution is to iterate like normal with a csv.reader
and whenever you find the desired row, you create dictionary and append it:
import csv
import json
def get_academy_awards_nominees(year):
result = []
with open("csvs/the_oscar_award.csv") as f:
reader = csv.reader(f)
header = next(reader)
for row in reader:
if int(row[1]) == year:
result.append(dict(zip(header, row)))
return json.dumps(result)
print(get_academy_awards_nominees(1928))
Which one is better?
It depends. In first approach, if you delete year_film
column for example, nothing gets wrong, because it deals with keys rather than index. But it second approach you have to check the indexes.
If your file is big enough and you just one a few rows, (use database instead generally or) I guess the second approach would be less costlier. Generating a dictionary from those keys require more work compare to generating a list. Prove it yourself since I didn't time it.