I have a buffer that looks like that:
some_random_info
another info here that i dont want to parser
column1,column2,column3
a,b,c
I want to read this that using python csv built-in module using the DictReader
class, but from the docs it says:
class csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
Create an object that operates like a regular reader but maps the information in each row to a dict whose keys are given by the optional fieldnames parameter.
The fieldnames parameter is a sequence. If fieldnames is omitted, the values in the first row of file f will be used as the fieldnames. Regardless of how the fieldnames are determined, the dictionary preserves their original ordering.
I have tried that:
import io
import csv
buffer = io.StringIO("""
some_random_info
another info here that i dont want to parser
column1,column2,column3
a,b,c
""")
reader = csv.DictReader(buffer,fieldnames=['column1','column2','column3'])
for row in reader:
print(row)
But outputs this:
{'column1': 'some_random_info', 'column2': None, 'column3': None}
{'column1': 'another info here that i dont want to parser', 'column2': None, 'column3': None}
{'column1': 'column1', 'column2': 'column2', 'column3': 'column3'}
{'column1': 'a', 'column2': 'b', 'column3': 'c '}
what I'm looking for is just {'column1': 'a', 'column2': 'b', 'column3': 'c '}