Answer the question
In order to leave comments, you need to log in
How to write a csv reader in python?
The task is to write a function to read a CSV file with the delimiter ' , ' and return a list of records.
I wrote a function but there is an error with a separator
def csvReader(filename):
records = []
for line in open(filename):
line = line.rstrip()
if line=='':
continue
records.append([line])
return records
Answer the question
In order to leave comments, you need to log in
Here is the solution of the functions, I think there is someone who needs it
import csv
def csvReader(filename):
records = []
with open(filename,'r') as f:
csv_reader = csv.reader(f,delimiter = ',')
for line in csv_reader:
if line=='':
continue # ignore empty line
records.append(line)
return records
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question