擅长:python、mysql、java
<p>您可以在文件中的<code>dict</code>和<code>print</code>之间循环:</p>
<pre><code>with open(filename.csv, 'w') as fh:
for k, v in Inventory.items():
for k1, v1 in v.items():
print(k, k1, *v1.values(), file=fh, sep=', ')
</code></pre>
<p>或者在理解上:</p>
<pre><code>with open(filename.csv, 'w') as fh:
print(*(', '.join((k, k1, *v1.values()))
for k, v in Inventory.items()
for k1, v1 in v.items()),
file=fh, sep='\n'
)
</code></pre>
<p>输出:</p>
<pre><code>devicename, Interface1, value, value
devicename, Interface2, value, value
</code></pre>