1

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=? 
...
alexsuv
  • 35
  • 8

2 Answers2

3

Based on your updated question, is this better?

import csv

fileName = 'csv_file_test.csv'
with open(fileName, 'r', encoding='UTF-8', newline='') as csvfile:
    reader_d = csv.DictReader(csvfile)

    for num, row in enumerate(reader_d, 1):
        data = ', '.join(f'{name}={value}' for name, value in row.items())
        print(f'for the row#{num}: {data}')

Print output:

for the row#1: field1=11, field2=12, field3=13
for the row#2: field1=21, field2=22, field3=23
martineau
  • 119,623
  • 25
  • 170
  • 301
  • I just edited my post with more details, sorry I was not clear. – alexsuv May 01 '20 at 00:45
  • Creating variables dynamically is almost always a bad idea — and is usually unnecessary, as illustrated in my updated answer. – martineau May 01 '20 at 01:02
  • `row` is dictionary that has the field-names as keys and each is associated with the corresponding value. i.e. For the first iteration: `row['field1']` is `11`, `row['field2']` is `12`, etc. In other words they are already "separate". You can use `row.items()` to iterate through these (key. value) pairs, _without_ knowing the field names, so that's how you should process them — not by creating variables (whose names you don't know in advance) with the values in them. – martineau May 01 '20 at 01:15
  • How do I extract "field1" name and "11" value (separately) in each row? row#2: field1 (I will do something...) and separately "11" (I will do something)? – alexsuv May 01 '20 at 01:18
  • `for row in reader_d:`, then `for field, value in row.items():`. `field` will contain the name of the CSV column, and `value` with be have the corresponding value. – martineau May 01 '20 at 01:21
0

Something like pandas may be able to help you out. You could do something like

import pandas as pd

df = pd.read_csv(your_csv_file_here)

print(df[col_X])

link to the documentation for read_csv - https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

RohanP
  • 352
  • 1
  • 5