<p>另一种方法是将Excel电子表格转换为CSV格式。在</p>
<p>然后使用标准库中的Python<a href="https://docs.python.org/2/library/csv.html" rel="nofollow">csv</a>模块。在</p>
<pre><code>from csv import reader as csv_reader
with open("data.csv", "r") as fd:
for row in csv_reader(fd):
userid, firstname, lastname = row
</code></pre>
<p><strong>说明:</strong></p>
<ol>
<li>将<code>csv.reader</code>对象导入为<code>csv_reader</code>(<em>我的个人约定</em>)。在</li>
<li>使用上下文管理器和Python的内置<a href="http://legacy.python.org/dev/peps/pep-0343/" rel="nofollow">with</a>,打开<code>data.csv</code>读取<code>"r"</code>,将生成的file对象放入<code>fd</code>。在</li>
<li>现在迭代由CSV库处理提供的数据行,<code>csv_reader(file_object)</code>提供给我们。在</li>
<li>{cd7>我们可以把<cd7}的每一行都分解成<cd7}的形式。参见:<a href="https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences" rel="nofollow">Tuples and Sequences</a></li>
</ol>
<p><em>另请注意:</em>如果您的数据包含<a href="http://en.wikipedia.org/wiki/Unicode" rel="nofollow">Unicode</a>字符,您将希望安装并使用<a href="https://pypi.python.org/pypi" rel="nofollow">Python Package Index</a>(<em>PyPi</em>)中的<a href="https://pypi.python.org/pypi/unicodecsv" rel="nofollow">unicodecsv</a>第三方库。在</p>