擅长:python、mysql、java
<p><em>JSON<a href="https://stackoverflow.com/questions/4162642/python-single-vs-double-quotes-in-json">requires double quotes for strings</a></em>。这意味着您不能使用<code>json</code>按原样加载它</p>
<p>我将使用<a href="https://docs.python.org/2/library/csv.html" rel="nofollow noreferrer">^{<cd2>}</a>解析以管道分隔的文件,并使用<a href="https://docs.python.org/2/library/ast.html#ast.literal_eval" rel="nofollow noreferrer">^{<cd3>}</a>将最后一列值安全地加载到Python字典中:</p>
<pre><code>import csv
from ast import literal_eval
with open("file.csv") as f:
reader = csv.reader(f, delimiter="|")
data = [literal_eval(line[-1]) for line in reader]
print(data) # data contains a list of dictionaries now
</code></pre>