【发布时间】:2016-07-18 10:38:24
【问题描述】:
我在尝试将某个目录中的 .txt 文件中的数据提取到 Spyder 时遇到问题。基本思想是我在 .txt 文件中有一个数据表,并且我希望某些列在 Python 中用作数组。我正在使用 numpy 函数 genfromtxt() 来执行此操作,当我将其编写为程序然后在 Spyder 中运行时它工作正常。但是,我想与数组进行更多交互,并且由于文件非常大,每次我想对数组进行任何小的更改时都需要一分钟左右的时间来加载。最终,我将使用更大的数组,所以现在解决这个问题会有所帮助。目前,我正在使用 Spyder 2.3.5.2。如果我尝试使用控制台加载 .txt 文件,则会收到以下错误:
>>> import numpy as np
>>> allPlanetHosts = np.genfromtxt('planetHost.txt', comments='#', skip_header=5, usecols=(1,2,3,4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1364, in genfromtxt
fhd = iter(np.lib._datasource.open(fname, 'rbU'))
File "C:\Python27\lib\site-packages\numpy\lib\_datasource.py", line 151, in open
return ds.open(path, mode)
File "C:\Python27\lib\site-packages\numpy\lib\_datasource.py", line 501, in open
raise IOError("%s not found." % path)
IOError: planetHost.txt not found.
我将目录设置为与planethost.txt 文件所在的目录相同的目录(在右上角的栏中,我通过粘贴正确的目录更改了目录),但是我还需要做些什么吗为了让它指向正确的目录?谢谢
【问题讨论】:
-
这很奇怪。这是你所期望的输出吗?
import os; os.getcwd() -
你应该将完整路径传递给
'planetHost.txt'