I have a CSV file that has 3 columns. Let's say: a
, b
, c
. I'm using csv.dictReader
to read it and add another column that has just the name of the file on each row.
This is my function:
def addFilename(self):
with open(self.datafile, "r") as f:
reader = csv.DictReader(f, delimiter='|')
for a, b, c in reader:
#Get filename
filename = self.getFilename()
yield {
"_source": {
"a": a,
"b": b,
"c": c,
"filename": filename
}
}
Now I'd like to generalize that behavior for many different CSV files. Those files have different number of columns and different column names. Is there a way to do so?
I don't want to modify the CSV file. The only thing I know is that I can get the fieldnames (and the number of fields) using reader.fieldnames
, but I don't know how I could use that in a yield
.