擅长:python、mysql、java
<p>您可以使用<code>file.tell</code>方法在读取和跳过行时保存文件指针位置,直到找到标题行,此时您可以使用<code>file.seek</code>方法将文件指针重置回标题行的开头,以便<code>csv.DictReader</code>可以将文件的其余部分解析为有效的CSV:</p>
<pre><code>with open(file, 'r') as data_file:
while True:
position = data_file.tell()
line = next(data_file)
if line.count(',') == 3: # or whatever condition your header line satisfies
data_file.seek(position) # reset file pointer to the beginning of the header line
break
data_pipe = csv.DictReader(data_file)
...
</code></pre>