A
A
Anonymous Anonymous2019-12-03 23:44:55
Python
Anonymous Anonymous, 2019-12-03 23:44:55

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

2 answer(s)
A
Anonymous Anonymous, 2019-12-10
@King_Of_Demons

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

D
Dimonchik, 2019-12-04
@dimonchik2013

see csv.reader and csv.register_dialect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question