<p>我做了一些计算。我想从数据中提取两列,并用python将它们保存在另一个文件中。在</p>
<p>到目前为止,我所做的就是:将文件保存为.txt,然后编写以下脚本:</p>
<pre><code># -*- coding: utf-8 -*-
import csv
f = open('file.txt')
csv_f=csv.reader(f)
for row in csv_f:
print row[1:3] # to get the second and third columns
f.close()
</code></pre>
<p>问题是当我运行脚本时:我得到了这个错误:<code>IndexError: list index out of range</code>。在</p>
<p>我已经知道问题出在哪里了,因为它将每一行的所有结果显示为列表中的on字符。我怎么也不知道怎么解决这个问题,把它们分开排成一排。在</p>
<p>以文件的前两行为例:</p>
^{pr2}$
<p>但这是我使用print row时得到的结果:</p>
<pre><code>['Ene: 1 -0.429787341139E+03 -0.42979E+03 -0.59461E+01 4296 0.664E+00 0.167E+01']
['Ene: 2 -0.395935688219E+03 0.33852E+02 -0.43868E+01 4356 0.711E+00 0.928E+00']
</code></pre>
<p>如果你能帮我解决这个问题,我会非常感激的。在</p>
<pre><code>import csv
f = open('file.txt')
csv_f=csv.reader(f)
for row in csv_f:
print row.split(" ").[1:3] # to get the second and third columns
f.close()
</code></pre>