擅长:python、mysql、java
<p>您需要将数据拆分为标题,然后再拆分(数据的)行。在</p>
<pre><code>header = ['country name', 'average urban population ratio', 'average life expectancy', 'sum of total population in all years', 'sum of urban population in all years']
final_list = []
for key, value in sorted(stats_dict.iteritems()):
if value[5] != 0:
final_list.append([key, value[4], value[5], value[0], value[1]])
with open('output.csv', 'wb') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(header)
writer.writerows(final_list) # note .writerows
# f.close() - not needed, the with statement closes the file for you
</code></pre>