擅长:python、mysql、java
<p>你需要像这样使用拉链:</p>
<pre><code>import requests
with open('TZ.txt') as file:
tz_lines = file.readlines()
with open ('TZContents.txt') as file2:
tz_contents = file2.readlines()
for name, contents in zip(tz_lines, tz_contents):
url = 'https://dashboard.meraki.com/api/v0/networks/%s' % name.rstrip("\n")
headers = {'X-Cisco-Meraki-API-Key': 'API KEY','Content-Type': 'application/json'}
response = requests.request('PUT', url, headers=headers, data='{%s}' % contents, allow_redirects=True, timeout = 10)
print(response.text)
</code></pre>
<p>这也是非常容易出错的。如果可能的话,最好以一种不依赖于完美排列的方式生成源数据。要捕获可能的错误,可以尝试以下操作:</p>
<pre><code>if len(tz_lines) != len(tz_contents):
raise RuntimeError("Files are not the same length!")
</code></pre>
<p>但最理想的情况是,首先要把所有的数据放在一起。将所有内容保存为JSON将是理想的:</p>
<pre><code>[
{"name": "the name string", "payload": {"your": "payload"}},
"more rows"
]
</code></pre>
<p>然后可以在这些<code>with</code>块中使用<code>json.load(file)</code>。而且请求对JSON有很好的支持,所以您可以像传递文件内容一样直接传递解码后的JSON。你知道吗</p>