擅长:python、mysql、java
<p>我想你可能在Python2.x上,但也许这会有所帮助。我填充了一个虚拟的<code>stats_dict</code>并重新排列了值的索引(我称之为<code>v</code>)。我还为你做了一个列表列表。在</p>
<pre><code>final_list = [['country name', 'average urban population ratio', 'average life expectancy', 'sum of total population in all years', 'sum of urban population in all years']]
stats_dict = {'Key': ['USA', 250000000, 75, 1000000, 1000001]}
for k, v in sorted(stats_dict.items()):
if v[4] != 0:
final_list.append([v[0], v[1], v[2], v[3], v[4]])
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=',')
writer.writerows(final_list)
</code></pre>