Gopi Shankar
Gopi Shankar

Reputation: 67

Why an I getting redudant "None" element in python?

I have this code which I am using to read my dataset from csv file with the CSV module:

keys = []

def ahencrypt(row):
    for field in row:
        print field

def read_data(filename):
    import csv
    with open(filename, "rb") as csvfile:
        datareader = csv.reader(csvfile)
        for row in datareader:
            yield row

for row in read_data('dataset/car.data'):
        print ahencrypt(row)

My data has just 7 columns, but after reading each row the program is giving my redudant None value. I can't understand the problem here. Can anyone please have a look at it?

PS: I am using this dataset

Upvotes: 1

Views: 54

Answers (2)

Jon Kiparsky
Jon Kiparsky

Reputation: 7753

Your ahencrypt function prints a line and implicitly returns a None. You then print that None in this loop:

for row in read_data('dataset/car.data'):
        print ahencrypt(row)

Upvotes: 0

BrenBarn
BrenBarn

Reputation: 251468

Your ahencrypt function prints things and returns None. Your code at the end prints the return value of ahencrypt, which is None. You can just remove the print in the last line of your code.

Upvotes: 1

Related Questions