[英]python parse from text/csv file to request.post api
我想将下面的 curl 命令转换为 python 脚本。 因为我需要从文本或 csv 加载数据信息。 但是我很难从文件中解析数据。 有人可以帮助我理解这一点。 谢谢你。
curl -X POST -d '{"hostname":"localhost.localdomain","version":"v2c","community":"public"}' -H 'X-Auth-Token: my_api_token' https:// xxx/api/v0/设备
我的代码:
import requests
import json
import csv
auth_token = "my_api_token"
api_url_base = 'http://xxxx/api/v0/devices'
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer {0}'.format(auth_token)}
def add_device(name, filename):
with open(nodes.csv, 'r') as f:
add_device = f.readline()
add_device = {'hostname': $name, 'version': %version, 'community': %community}
response = requests.post(api_url_base, headers=headers, json=add_device)
print(response.json())
您应该拆分 CSV 内容。 COUNT 显示 CSV 文件中以逗号分隔的项目数。
add_devices = []
with open(nodes.csv, 'r') as f:
line = f.readline():
items = line.split(',', COUNT) # if COUNT is 3 then
add_devices = {'hostname': items[0], 'version': items[1], 'community': items[2]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.