I need to loop through the CSV rows like this:
top row, row2
top row, row3
...etc
More specifically:
Loop starts:
First iteration:
Get the top row#1 (header)
Do something, extractions etc
Get the row#2
Do something, extractions etc
Second iteration:
Get the top row#1 (header)
Do something, extractions etc
Get the row#3
Do something, extractions etc
Third iteration:
Get the top row#1 (header)
Do something, extractions etc
Get the row#4
Do something, extractions etc
...etc...
Loop finishes
My idea is (maybe there is a better idea):
Input CSV:
field1,field2,field3
11,12,13
21,22,23
import csv
fileName = 'csv_file_test.csv'
with open(fileName, 'r', encoding='UTF-8') as csvfile:
reader_d = csv.DictReader(csvfile)
header_d = next(reader_d)
print("header_d: ")
print(header_d)
for row in reader_d:
print(row)
And the result is not bad, I just need help to extract (iterating) each element from this dict, please:
header_d:
OrderedDict([('field1', '11'), ('field2', '12'), ('field3', '13')])
OrderedDict([('field1', '21'), ('field2', '22'), ('field3', '23')])
I do not know how many columns, so I have to go through every column for each row starting from row#2 in each iteration. So I basically need the column name with column value, for each row, e.g.:
I need to find the column name and its corresponding value for each row:
for the row#2: column name=? and value=?
for the row#3: column name=? and value=?
...