无法使用 pandas 在 python3 中加载 csv 文件
问题描述
大家好,我不知道为什么,但是 pandas 确实无法在我的 python 代码中加载我的 csv 文件.该文件位于我的 python 项目的同一文件夹中,我正在使用 tensorflow、theano、pandas、numpy 和 matplotlib 库.已经 2 小时了,我尝试使用 pip3 命令卸载所有内容并再次重新安装.我卸载了 tensorflow、pandas、theano、numpy,甚至还卸载了 python2 和 python3.没有什么.我还更新和升级了我的 linux 发行版.无用.
Hi guys I don't know why but pandas really can't manage to load my csv file in my python code. The file is in the same folder of my python project, I'm working with tensorflow, theano, pandas, numpy and matplotlib libraries. It's been 2 hrs and I tried uninstalling everything and reinstalling once again using the pip3 commands. I uninstalled tensorflow, pandas, theano, numpy, and even python2 and python3. Nothing. I also updated and upgraded my linux distro. useless.
这是我在 Spyder3 内部终端中执行部分代码时不断遇到的错误:
this is the error I keep getting when I execute the part of my code in the Spyder3 internal terminal:
dataset = pd.read_csv('Churn_Modelling.csv')
Traceback (most recent call last):
File "<ipython-input-4-610b2f33ea04>", line 1, in <module>
dataset = pd.read_csv('Churn_Modelling.csv')
File "/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py", line 709, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py", line 449, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py", line 818, in __init__
self._make_engine(self.engine)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py", line 1049, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/local/lib/python3.6/dist-packages/pandas/io/parsers.py", line 1695, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 402, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 718, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'Churn_Modelling.csv' does not exist
这就是该死的代码行:
dataset = pd.read_csv('Churn_Modelling.csv')
这有什么问题吗?我也尝试重命名它.
What is wrong with it guys?? I also tried to rename it.
解决方案
错误信息是什么FileNotFoundError: File b'Churn_Modelling.csv' does not exist
.如果文件存在,那么它不在你的 python 脚本的同一个工作目录中.
The error message is what it is FileNotFoundError: File b'Churn_Modelling.csv' does not exist
. If the file exists, then it is not in the same working directory of your python script.
我会尝试打印当前工作目录来检查python是否在正确的目录中寻找文件.
I would try printing the current working directory to check whether python is looking for the file in the correct directory.
import os
print(os.getcwd())
或者,您可以使用绝对路径,而不是使用相对路径(例如,Churn_Modelling.csv
)导入文件
Alternatively, instead of importing the file using a relative path (e.g., Churn_Modelling.csv
), you could use an absolute path
dataset = pd.read_csv('/path/to/file/Churn_Modelling.csv')
另外,请注意在 POSIX 系统(例如,Linux/Mac)中,文件名区分大小写(即,Churn_Modelling
与 churn_modelling
不同).
Also, be mindful that in POSIX systems (e.g., Linux / Mac), the file name is case sensitive (i.e., Churn_Modelling
is not the same as churn_modelling
).
相关文章