[Tutor] Reading a CSV file

alexkleider alexkleider at protonmail.com
Fri Jun 12 21:30:22 EDT 2020


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Friday, June 12, 2020 10:59 AM, John Weller <john at johnweller.co.uk> wrote:

> I have a CSV file with 366 rows each with 4 comma separated fields; day_no,
> date, sunrise, sunset. The top row contains the field names. I want access
> the time of sunrise and sunset from file for a particular day_no.
> I have tried:
> import csv
> with open('sun_times_gmt.csv', newline= '') as csvfile:
> sun_times = csv.DictReader(csvfile, delimiter=',')
> for row in sun_times:
> print(row['sunrise'])
> but it prints out the entire column. How do I specify a particular row?
> TIA
> John Weller

Try adding an if statements as follows:

for row in sun_times:
    if row['day_no'] == my_day_number:
        print(row['sunrise'])




More information about the Tutor mailing list