擅长:python、mysql、java
<p>输入文件的格式不便于用<code>csv</code>模块解析。我将分别解析头,然后逐行解析rest,通过<code>,</code>分割,一路上剥离并生成字典。工作代码:</p>
<pre><code>from pprint import pprint
d = {}
with open("myfile.csv") as f:
headers = [header.strip() for header in next(f).split(",")[1:]]
for line in f:
values = [value.strip() for value in line.split(",")]
d[values[0]] = dict(zip(headers, values[1:]))
pprint(d)
</code></pre>
<p>印刷品:</p>
<pre><code>{'row1': {'col1': '23', 'col2': '42', 'col3': '77'},
'row2': {'col1': '25', 'col2': '39', 'col3': '87'},
'row3': {'col1': '48', 'col2': '67', 'col3': '53'},
'row4': {'col1': '14', 'col2': '48', 'col3': '66'}}
</code></pre>