回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我想<code>csv.DictReader</code>从文件中推断字段名。<a href="http://docs.python.org/library/csv.html#csv.DictReader" rel="nofollow noreferrer">The docs</a>说<em>“如果省略fieldnames参数,csvfile的第一行中的值将用作字段名。”</em>,但在我的例子中,第一行包含标题,第二行包含名称。</p>
<p>我无法根据<a href="https://stackoverflow.com/questions/4787723/python-3-2-skip-a-line-in-csv-dictreader">Python 3.2 skip a line in csv.DictReader</a>应用<code>next(reader)</code>,因为在初始化读卡器时发生了字段名分配(或者我做错了)。</p>
<p>csvfile(从Excel 2010导出,<a href="http://ftp2.cits.rncan.gc.ca/pub/canvec/doc/CanVec_EntitiesCodesFileNames_EntitesCodesNomsDeFichier_v1_1_1.xls" rel="nofollow noreferrer">original source</a>):</p>
<pre><code>CanVec v1.1.0,,,,,,,,,^M
Entity,Attributes combination,"Specification Code
Point","Specification Code
Line","Specification Code
Area",Generic Code,Theme,"GML - Entity name
Shape - File name
Point","GML - Entity name
Shape - File name
Line","GML - Entity name
Shape - File name
Area"^M
Amusement park,Amusement park,,,2260012,2260009,LX,,,LX_2260009_2^M
Auto wrecker,Auto wrecker,,,2360012,2360009,IC,,,IC_2360009_2^M
</code></pre>
<p>我的代码:</p>
<pre><code>f = open(entities_table,'rb')
try:
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0)
reader = csv.DictReader(f, dialect=dialect)
print 'I think the field names are:\n%s\n' % (reader.fieldnames)
i = 0
for row in reader:
if i < 20:
print row
i = i + 1
finally:
f.close()
</code></pre>
<p>当前结果:</p>
<pre><code>I think the field names are:
['CanVec v1.1.0', '', '', '', '', '', '', '', '', '']
</code></pre>
<p>预期结果:</p>
<pre><code>I think the field names are:
['Entity','Attributes combination','"Specification Code Point"',...snip]
</code></pre>
<p>我意识到简单地删除第一行并继续下去是很方便的,但是我正试图尽可能接近于就地读取数据,并尽量减少人工干预。</p>