python - Filter() on DictReader -


i'm new python , try comprehend how can use filter function on csv.dictreader filter rows csv file. filter() can used on "iterable" , far understand dictreader fits definition.

however when try

f = open('file1.csv', 'r')        dialect = csv.sniffer().sniff(f.read(1024)) f.seek(0) reader = csv.dictreader(f, none, none, none, dialect)  filteredreader = filter(none, reader) #none replaced function in filteredreader:     print(i) 

i typeerror: normcase() argument must str or bytes, not 'dictreader'.

please note, don't want filter on filepointer (e.g. here), on parsed csv rows. have idea how that?

yes, dictreader() can used iterable, , can used filter() fine.

the filter() function passed each row (a dictionary) in turn , if function returns true row passed on:

>>> io import stringio >>> import csv >>> demo = stringio('''\ ... foo,bar,baz ... 42,88,131 ... 17,19,23 ... ''') >>> reader = csv.dictreader(demo) >>> def only_answers(row): ...     return '42' in row.values() ...  >>> row in filter(only_answers, reader): ...     print(row) ...  {'baz': '131', 'bar': '88', 'foo': '42'} 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -