如何使用CSV文件中的变量循环多次迭代?

t30tvxxf  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(125)

我尝试在迭代中运行以下代码,从名为sample.csv的CSV文件中提取VAR1和VAR2,并将它们插入下面的匹配点,然后对CSV文件中的每一行循环该函数。

import http.client
import json

conn = http.client.HTTPSConnection("a.b.com")
payload = json.dumps({
  "node_list": [
    {
      "id": VAR1,
      "with_descendants": True
    }
  ]
})
headers = {
  'Content-Type': 'application/json',
  'Cookie': 'A'
}
conn.request("PUT", "/poweruser/v1/powerusers/VAR2/branches?access_token=xyz", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Sample.csv:
变量1、变量2 10,20 30,40 50,60 70,80

t1qtbnec

t1qtbnec1#

你可以使用csv模块来解析这些行。

with open('sample.csv','r') as file:
    reader = csv.reader(file)
    next(reader) # this is to skip the first line in the csv file
    for VAR1, VAR2 in reader:
        print(f"A code with VAR1={VAR1}, and VAR2={VAR2}")

用您的代码替换print语句。

相关问题