CSV 遍历行

CSV loop through rows

像大多数人一样,我正在尝试学习 python 以促进我的职业发展等...作为其中的一部分,我们正在考虑在工作中部署 Cisco Meraki。由于我们将拥有大量网络,因此能够通过 API.

从 csv 文件部署信息会很棒

我已经设法使下面的代码在一定程度上起作用。它会 运行 成功,但只有 运行 是 CSV 的最后一行而不是每一行,直到没有行为止。

CSV 大约有 130 行子网、IP 地址和网络 ID

我很接近,但我似乎无法理解最后一点。

    # actionBatch-VlanUpdate.py

import requests
import csv

# Environment Variables
API_KEY = "xxxxxx"
org_id = xxxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"

with open('Subnets.csv', newline="\n") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
     net_id = row[3]
     V5NET = row[4]
     V5GW = row[5]
     V10NET = row[6]
     V10GW = row[7]
     V11NET = row[8]
     V11GW = row[9]



payload = {
    "confirmed": True,
    "synchronous": True,
    "actions": [
        {
            "resource": f"/networks/{net_id}/vlans",
            "operation": "create",
            "body": {
                "id": "5",
                "name": "Data",
                "applianceIp": f"{V5GW}",
                "subnet": f"{V5NET}/24",
            },
        },
        {
            "resource": f"/networks/{net_id}/vlans",
            "operation": "create",
            "body": {
                "id": 10,
                "name": "Voice",
                "applianceIp": f"{V10GW}",
                "subnet": f"{V10NET}/24",
            },
        },
        {
            "resource": f"/networks/{net_id}/vlans",
            "operation": "create",
            "body": {
                "id": 11,
                "name": "Property",
                "applianceIp": f"{V11GW}",
                "subnet": f"{V11NET}/24",
            },
        },
    ],
}

headers = {
    "X-Cisco-Meraki-API-Key": API_KEY,
    "Content-Type": "application/json",
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

根据反馈编辑的版本

# actionBatch-VlanUpdate.py

import requests
import csv

# Environment Variables
API_KEY = "XXXX"
org_id = xxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"

with open('Subnets.csv', newline="\n") as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
     net_id = row[3]
     V5NET = row[4]
     V5GW = row[5]
     V10NET = row[6]
     V10GW = row[7]
     V11NET = row[8]
     V11GW = row[9]
        payload = {
            "confirmed": True,
            "synchronous": True,
            "actions": [
                {
                    "resource": f"/networks/{net_id}/vlans",
                    "operation": "create",
                    "body": {
                        "id": "5",
                        "name": "Data",
                        "applianceIp": f"{V5GW}",
                    "subnet": f"{V5NET}/24",
                    },
                },
                {
                    "resource": f"/networks/{net_id}/vlans",
                    "operation": "create",
                    "body": {
                        "id": 10,
                        "name": "Voice",
                        "applianceIp": f"{V10GW}",
                        "subnet": f"{V10NET}/24",
                    },
                },
                {
                    "resource": f"/networks/{net_id}/vlans",
                    "operation": "create",
                    "body": {
                        "id": 11,
                        "name": "Property",
                        "applianceIp": f"{V11GW}",
                        "subnet": f"{V11NET}/24",
                    },
                },
            ],
        }

headers = {
    "X-Cisco-Meraki-API-Key": API_KEY,
    "Content-Type": "application/json",
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

在您的 for 循环中,您将覆盖之前每次迭代的结果,因此它始终是文件中的最后一行。您可以使用 .append() 数组方法将每个循环的结果插入到各种数组的末尾。

编辑 - 看起来您在 for 循环结束之前进行了数组声明。如果你只想调用 requests 130 次,你只需要更改缩进,这样你的 requests 调用就在你的 for 循环中。

编辑 2 - 您还想在 for 循环中获取请求调用。

    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        net_id = row[3]
        V5NET = row[4]
        V5GW = row[5]
        V10NET = row[6]
        V10GW = row[7]
        V11NET = row[8]
        V11GW = row[9]
        payload = {
            "confirmed": True,
            "synchronous": True,
            "actions": [
                ...
            ],
        }

        headers = {
            "X-Cisco-Meraki-API-Key": API_KEY,
            "Content-Type": "application/json",
        }

        response = requests.post(url, json=payload, headers=headers)
        print(response.text)