用于将文件中的项读入json对象的python脚本
我有一个IPs文件。我需要检查每个条目并将它们写入json对象。我的JSON对象将“ip”:“x.x.x.x”、“subnet”:“y”和操作作为参数。IP和子网值将写入文件中。最后,我使用curl通过API调用修补这些JSON对象。有没有想过我该如何做到这一点 示例输入文件为: 1.1.1.1/24 2.2.2.2/16等等 我走了这么远用于将文件中的项读入json对象的python脚本,python,json,for-loop,curl,patch,Python,Json,For Loop,Curl,Patch,我有一个IPs文件。我需要检查每个条目并将它们写入json对象。我的JSON对象将“ip”:“x.x.x.x”、“subnet”:“y”和操作作为参数。IP和子网值将写入文件中。最后,我使用curl通过API调用修补这些JSON对象。有没有想过我该如何做到这一点 示例输入文件为: 1.1.1.1/24 2.2.2.2/16等等 我走了这么远 import os import requests import urllib3 # check if size of file is 0 if os.s
import os
import requests
import urllib3
# check if size of file is 0
if os.stat("ip.txt").st_size == 0:
print('File is empty')
else:
print('File is not empty')
url = 'https://api.server.com/service/6sxyz/acl/5xUrx/entries'
payload = open("request.json")
head = {'content-type': 'application/json', 'X-Key': 'xxxx'}
with open('ip.txt', 'r') as f:
for line in f:
for word in line.split():
if(len(word)>2):
ip=word
else:
subnet=word
print(subnet)
data = {'op': 'create', 'ip': ip,'subnet': subnet}
r = requests.patch(url, data, headers=head)
我得到这个错误
Traceback (most recent call last):
File "ip_curl.py", line 13, in <module>
payload = open("request.json")
IOError: [Errno 2] No such file or directory: 'request.json'
回溯(最近一次呼叫最后一次):
文件“ip_curl.py”,第13行,在
有效负载=打开(“request.json”)
IOError:[Errno 2]没有这样的文件或目录:“request.json”
有什么建议吗?我现在没有错误了。但是补丁调用不会产生所需的结果
import os
import requests
import urllib3
import json
# check if size of file is 0
if os.stat("ip.txt").st_size == 0:
print('File is empty')
else:
print('File is not empty')
url = 'https://api.server.com/service/6sxyz/acl/5xUrx/entries''
head = {'content-type': 'application/json', 'X-Key': 'xxxx'}
with open('ip.txt', 'r') as f:
for line in f:
ip = ""
subnet = ""
for word in line.split():
if(len(word)>2):
ip=word
else:
subnet=word
print(subnet)
data = {'op': 'create', 'ip': ip,'subnet': subnet}
print data
r = requests.patch(url, data, headers=head)
这是必需的curl,它独立于脚本或文件输入工作-
curl -H "X-Key: " -H "Content-type: application/json" -X PATCH https://api.server.com/service/6sxyz/acl/5xUrx/entries' -d '{"entries":[{"op": "create", "ip": "192.168.0.1","subnet": "8"},{"op": "create", "ip": "192.168.0.2", "subnet": "16"}]}'
我现在没有错误了。但是补丁调用不会产生所需的结果
import os
import requests
import urllib3
import json
# check if size of file is 0
if os.stat("ip.txt").st_size == 0:
print('File is empty')
else:
print('File is not empty')
url = 'https://api.server.com/service/6sxyz/acl/5xUrx/entries''
head = {'content-type': 'application/json', 'X-Key': 'xxxx'}
with open('ip.txt', 'r') as f:
for line in f:
ip = ""
subnet = ""
for word in line.split():
if(len(word)>2):
ip=word
else:
subnet=word
print(subnet)
data = {'op': 'create', 'ip': ip,'subnet': subnet}
print data
r = requests.patch(url, data, headers=head)
这是必需的curl,它独立于脚本或文件输入工作-
curl -H "X-Key: " -H "Content-type: application/json" -X PATCH https://api.server.com/service/6sxyz/acl/5xUrx/entries' -d '{"entries":[{"op": "create", "ip": "192.168.0.1","subnet": "8"},{"op": "create", "ip": "192.168.0.2", "subnet": "16"}]}'
你能提供一个输入和所需的输出文件吗?关于如何实现这一点有什么想法吗?是的,试着自己做,如果遇到特定的技术问题,请回来。在那之前,这太宽泛/模糊了。谢谢你的评论。非常有用。请删除显示错误的行,然后重试。您的代码中没有任何其他内容依赖于它:-dw什么是
request.json
for?你根本不用它。问题是该文件不存在,open('request.json')
试图打开该文件进行读取,而不是写入,因此出现错误,因为该文件不存在。如果要向文件写入内容,必须使用open('request.json',mode='w')
。但是,正如前面的评论员所指出的,您根本没有使用该文件做任何事情,因此您最好放弃它。您能提供一个输入和所需的输出文件吗?我如何实现这一点有什么想法吗?是的,试着自己做,如果遇到特定的技术问题,请回来。在那之前,这太宽泛/模糊了。谢谢你的评论。非常有用。请删除显示错误的行,然后重试。您的代码中没有任何其他内容依赖于它:-dw什么是request.json
for?你根本不用它。问题是该文件不存在,open('request.json')
试图打开该文件进行读取,而不是写入,因此出现错误,因为该文件不存在。如果要向文件写入内容,必须使用open('request.json',mode='w')
。但是,正如前面的评论员所指出的,您根本没有使用该文件做任何事情,所以您最好放弃它。我得到400个响应。添加了这行代码:data=json.dumps(payload),现在它可以工作了。我的json格式不正确。我得到200个响应。我得到400个响应。添加了这一行:data=json.dumps(有效负载),它现在可以工作了。我的json格式不正确。我得到200个回复。