gpt4 book ai didi

python - CSV 循环遍历行

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:59 27 4
gpt4 key购买 nike

像大多数人一样,我正在尝试学习 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)

关于python - CSV 循环遍历行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58768965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com