2

csv

name;subnet
name_a;192.168.111.0/24
name_b;192.168.168.0/24
name_c;192.168.29.0/24

I try to run a function with a for loop for every column from a csv by passing the column values to function arguments. How should I do this?

I'm not sure if I have to import the csv content to a list, dict or if it is possible to directly use the columns as function arguments.

with open(csv_address_objects, mode='r', encoding='utf-8-sig') as csv_file:
    csv_reader = csv.DictReader(csv_file, delimiter=';')
    list_of_csv = dict(csv_reader)
SIDP
  • 23
  • 3

3 Answers3

1

Try this:

import pandas as pd

data = pd.read_csv("test.csv", sep=";")

names = data["name"].to_list()
subnets = data["subnet"].to_list()

def process_data(names, subnets):
    for name, subnet in zip(names, subnets):
        print(f"{name}: {subnet}")

if __name__ == "__main__":
    process_data(names, subnets)
blunova
  • 2,122
  • 3
  • 9
  • 21
  • Thanks. Now if I use a for loop in the function for every row should I do it like ` def process_data(name, subnet): for x in name: ` or is there a way to count up the rows? – SIDP Jun 08 '22 at 13:39
  • You are welcome! You can use a simple `for` loop as you mentioned so `for x in name:` is fine, or, if you want to be very pythonic, you can use list comprehensions. It depends on your needs. If you just need the length, just type `len(name)`. – blunova Jun 08 '22 at 13:42
  • Last question :). I need both values (name and subnet) in the loop but how would that for loop look like? This does not work: ` for x in name and y in subnet: print(f'name: {x}, subnet: {y}') ` and this neither: ` for x, y in name, subnet: ` – SIDP Jun 08 '22 at 14:01
  • Why split the data into separate lists, just to rejoin (zip) it later? I think because pandas—with its Series (or column) based approach to data—is the wrong tool for the simple job of iterating over a few rows. The CSV module which OP was already using is just about the perfect tool for this very simple task. Here's someone going so far as to say ["iter" in Pandas is an anit-pattern](https://stackoverflow.com/a/55557758/246801). – Zach Young Jun 09 '22 at 03:37
1

You were on the right track with just the code you had.

For the CSV module, reader and DictReader return an iterator over the "rows" of the CSV being read. In the case of DictReader, a row is a dict keyed to your CSV's column names (header), like:

{"name": "name_a", "subnet": "192.168.111.0/24"}

So, to iterate over your rows and use the two column values to pass to your function, you only need to make this small addition:

import csv

def my_func(name, subnet):
    print(f'doing something with name="{name}" and subnet="{subnet}"')

with open("input.csv", mode="r", encoding="utf-8-sig") as csv_file:
    csv_reader = csv.DictReader(csv_file, delimiter=";")

    for row in csv_reader:
        my_func(row["name"], row["subnet"])

        # or pass the dict's key-value pairs with `**` syntax
        # my_func(**row)

When I run that against your sample CSV, I get:

doing something with name="name_a" and subnet="192.168.111.0/24"
doing something with name="name_b" and subnet="192.168.168.0/24"
doing something with name="name_c" and subnet="192.168.29.0/24"
Zach Young
  • 10,137
  • 4
  • 32
  • 53
0

You should probably use pandas:

import pandas as pd
data = pd.read_csv(csv_address_objects, sep = ';')

You can then easily access columns, lines or any specific value. See: https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html

Thrasy
  • 536
  • 3
  • 9
  • I am not sure what you are trying to do but if you want to apply a function to each row, you can use: for index, row in data.iterrows(): function_to_apply(row) – Thrasy Jun 08 '22 at 13:42
  • Why should OP switch to pandas? They were quite close to getting the solution they desired. OP simply needs to iterate a few rows with a few columns, not plug into something that ["has the broader goal of becoming the most powerful and flexible open source data analysis/manipulation tool available in any language"](https://pandas.pydata.org/docs/getting_started/overview.html). – Zach Young Jun 09 '22 at 03:28