无法读取 .csv 文件 EmptyDataError:没有要从文件中解析的列

问题描述

我正在使用 MAC 笔记本电脑读取我的 .csv 文件,但出现此错误

EmptyDataError:没有要从文件中解析的列。

看看我的数据文件preview of the .csv file

我什至检查过文件文件路径是否正确,结果没问题。

path = 'Users\syedwaqar\Huma-IBM-ML\healthcare-dataset-stroke-data.csv' 
con = sq3.Connection(path)

我试图定义这样的路径,但它总是给出错误

path = 'Users/syedwaqar/Huma-IBM-ML/healthcare-dataset-stroke-data.csv'
con = sq3.Connection(path)

操作错误:无法打开数据库文件

之后:I tried to check if the filepath is correct,it shows that its correct. I wonder what the problem is.

这是写完这行代码后的主要错误

data = pd.read_csv(path)

----------------------------------------------- ----------------------------- EmptyDataError 回溯(最近一次调用 最后)在 ----> 1 data = pd.read_csv(path,header=None)

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py 中 read_csv(filepath_or_buffer,sep,分隔符,标题名称,index_col, 使用cols,挤压,前缀,mangle_dupe_cols,dtype,引擎,转换器, true_values、false_values、skipinitialspace、skiprows、skipfooter、 nrows、na_values、keep_default_na、na_filter、详细、 skip_blank_lines、parse_dates、infer_datetime_format、keep_date_col、 date_parser、dayfirst、cache_dates、迭代器、块大小、压缩、 千位,十进制,换行符,quotechar,引用,双引号, 转义字符,注释,编码,方言,error_bad_lines, warn_bad_lines,delim_whitespace,low_memory,memory_map, 浮点精度) 第684章 685 --> 686 返回 _read(filepath_or_buffer,kwds) 687 688

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py 中 _read(filepath_or_buffer,kwds) 450 451 # 创建解析器。 --> 452 解析器 = TextFileReader(fp_or_buf,**kwds) 453 454 如果块大小或迭代器:

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py 中 init(self,f,engine,**kwds) 944 self.options[“has_index_names”] = kwds[“has_index_names”] 945 --> 946 self._make_engine(self.engine) 947 948 def close(self):

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py 中 _make_engine(self,engine) 1176 def _make_engine(self,engine="c"): 1177 if engine == "c": -> 1178 self._engine = CParserWrapper(self.f,**self.options) 1179 else: 1180 if engine == "python":

~/opt/anaconda3/lib/python3.8/site-packages/pandas/io/parsers.py 中 init(self,src,**kwds) 2006 kwds["usecols"] = self.usecols 2007 -> 2008 self._reader = parsers.TextReader(src,**kwds) 2009 self.unnamed_cols = self._reader.unnamed_cols 2010

pandas/_libs/parsers.pyx 中 pandas._libs.parsers.TextReader.cinit()

EmptyDataError: 没有要从文件中解析的列

请帮我解决这个问题。我无法理解这个问题。

解决方法

您可能使用了错误的分隔符。这通常来自您的 Mac OS 语言和区域设置。

看看这篇文章,您将获得解决此问题所需的信息:https://harvestmedia.zendesk.com/hc/en-us/articles/360023978031-Opening-Excel-files-with-the-correct-CSV-list-separator

,

我尝试了很多方法来解决这个问题,但都无济于事。最后,在搜索了大量有关在 Pandas 数据框中读取 .csv 文件的信息后,我自己找到了解决方案。我发布我自己问题的答案只是为了帮助那些有同样问题的人。 无法读取 .csv 文件的原因有很多。必须检查其文件的预览,并根据文件的预览查找“pd.read_csv”函数中需要提及的所有参数,例如:分隔符类型(制表符分隔等)、空白标题(在那种情况标题=无)。在检查需要放置的任何必需参数后,如果问题仍然存在。那么问题可能出在文件路径上。输入

pwd

这将打印工作目录。然后你只需要在你的工作目录之后指定位置。例如 this shows how to specify the path of your file 指定工作目录后的路径。如果您的文件在工作目录中,那么只需像我一样提及文件名。 但是,如果您的文件存在于其他文件夹中,则您可以在工作目录之后指定后续文件夹 例如你的工作目录是“/Users/username” 并且您的文件位于“文档”中名为“huma”的文件夹中,那么您将编写以下代码:

path = 'Documents/huma/filename.csv'

或将工作目录更改为您的文件所在的文件夹。使用以下代码:

cd /Users/Documents/huma/ 

上面这行代码改变了我的工作目录,现在我只需要指定文件名:

path = 'filename.csv' 

您可以使用以下代码检查您的文件是否存在于所描述的路径中:

os.path.isfile('filename.csv')