擅长:python、mysql、java
<p><code>csv.reader</code>是iterables。链接两个iterable的最简单方法是使用<code>itertools.chain()</code>:</p>
<blockquote>
<p>chain(*iterables) > chain object</p>
<p>Return a chain object whose .<strong>next</strong>() method returns elements from the
first iterable until it is exhausted, then elements from the next
iterable, until all of the iterables are exhausted.</p>
</blockquote>
<p>所以在你的情况下:</p>
<pre><code>from itertools import chain
for row in chain(reader1, reader2):
proceed_with(row)*
</code></pre>
<p>现在给出了附加的解释(将行插入两个结构相似但名称不同的不同表中),这样做行不通。您可以尝试通过向读取器传递表名来实现智能和“通用”,即:</p>
<pre><code>for tablename, reader in (("table1", reader1), ("table2", reader2)):
for row in reader:
proceed_with(row, tablename)
</code></pre>
<p>但一旦一个或另一个csv和相关的表结构发生变化,就会中断。你知道吗</p>