using DictReader() with .decode('utf-8', 'ignore')

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Apr 14 09:23:12 EDT 2015


On Tue, 14 Apr 2015 10:54 pm, Vincent Davis wrote:

> I had been reading in a file like so. (python 3)
> with open(dfile, 'rb') as f:
>     for line in f:
> 
> line
>  = line.decode('utf-8', 'ignore').split(',')
> 
> How can I ​do accomplish decode('utf-8', 'ignore') when reading with
>  DictReader()


Which DictReader? Do you mean the one in the csv module? I will assume so.

I haven't tried it, but I think something like this will work:


# untested
with open(dfile, 'r', encoding='utf-8', errors='ignore', newline='') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row['fieldname'])



-- 
Steven




More information about the Python-list mailing list