Skip to content Skip to sidebar Skip to footer

Csv Reader Outputs Extra Blank Items

I have an input csv with a variable number of columns I'm trying to pull into a list. My test is parsing the input csv and creating a list with extra elements around the csv colum

Solution 1:

csv.reader takes a file-like object not a string...so it is iterating strangely over the characters of a line instead of the lines of a file. You just need:

from __future__ import print_function
import csv

withopen('conditions.lst','rb') as cf:
    r = csv.reader(cf,skipinitialspace=True)
    for line in r:
        print(line)

Output:

['string1:', 'string1b,string1c,']['stringa:', 'stringb,stringc,']['string3:', 'string3next=abc', 'string3b', 'string3c:', 'string3d']

Post a Comment for "Csv Reader Outputs Extra Blank Items"