0

Confused, need to "Write your code to print out the number of data records" - i know how to do this in pandas but not csv.DictReader

Have tried solutions from Number of lines in csv.DictReader and with len() with no success.

1 Answers1

0

Do you looking for to do this ?

sample.csv
"one","two","three"
4,5,6
7,8,9
import csv

with open("sample.csv") as f:
    r = csv.DictReader(f)

    print(len(list(r)))

output: 2
Ikarys
  • 66
  • 4
  • I'm here, import numpy as np DATA_FILE = "owid-covid-data.csv" input_file = csv.DictReader(open(DATA_FILE)) fieldnames = input_file.fieldnames data_dict = {fn: [] for fn in fieldnames} print(data_dict.keys()) for line in input_file: for k, v in line.items(): if (v == ''): v=0 try: data_dict[k].append(int(v)) except ValueError: try: data_dict[k].append(float(v)) except ValueError: data_dict[k].append(v) – Blake Settle Apr 18 '21 at 22:24
  • i've tried, ` print(len(list(input_file))) but it produced an output of 0 – Blake Settle Apr 18 '21 at 22:26
  • i am testing how to use the `code` function but it doesn't seem to be working, apologies for the text heavy explainer - just learning stackoverflow – Blake Settle Apr 18 '21 at 22:29
  • I retrieve the csv file "owid-covid-data.csv" and get some rows. ```import csv DATA_FILE = "owid-covid-data.csv" input_file = csv.DictReader(open(DATA_FILE)) print(len(list(input_file)))``` output: 28 # The number of rows in the CSV file. What do you want exactly ? – Ikarys Apr 19 '21 at 14:33