回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个CSV文件,头是键,数据是值。我的目标是将CSV文件转换成Json上传到数据库并输出我上传的数据。我已经成功地将CSV转换为Json,但是我的输出有问题。</p>
<p>我现在拥有的</p>
<pre><code>import csv
import json
import pandas as pd
csvfile = open ('so-emissions-by-world-region-in-million-tonnes.csv','r')
reader = csv.DictReader(csvfile)
result = []
for row in reader:
result.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(row)
result = json.dumps(result)
result = json.loads(result)
keys = ('Entity' ,'Year','SO2 emissions- Clio Infra')
print(result)
</code></pre>
<p>CSV数据:</p>
<pre class="lang-none prettyprint-override"><code>[{'502 emissions- Clio Infra': '0', 'Entity': 'Africa', 'Year': '1860 '},
{'502 emissions- Clio Infra': '0', 'Entity': 'Africa', 'Year': '1870'},
{'502 emissions- Clio Infra': '0.059', 'Entity': 'Africa', 'Year': '1880'},
{'502 emissions- Clio Infra': '0.065', 'Entity': 'Africa', 'Year': '1890'},
{'502 emissions- Clio Infra': '0.071', 'Entity': 'Africa', 'Year': ' 1900'},
{'502 emissions- Clio Infra': '0.146', 'Entity': 'Africa', 'Year': '1910'},
{'502 emissions- Clio Infra': '0.372', 'Entity': 'Africa', 'Year': '1920'},
{'502 emissions- Clio Infra': '0.41', 'Entity': 'Africa', 'Year': ' 1930'},
{'502 emissions- Clio Infra': '0.56 ', 'Entity': 'Africa', 'Year ': '1940'}]
</code></pre>
<p><a href="https://i.stack.imgur.com/qQ8tO.png" rel="nofollow noreferrer">This is the output of result</a></p>
<p>正确输出:</p>
<pre class="lang-none prettyprint-override"><code>'First Key'
Value 1
Value 2
Value 3
...
'Second Key'
Value 1
Value 2
Value 3
...
'Third Key'
Value 1
Value 2
Value 3
...
</code></pre>