首页 > 解决方案 > OSError:在 Pandas 中的 csv 上从文件初始化失败

问题描述

到目前为止pandas,我没有任何问题地阅读了我所有的 CSV 文件,但是现在似乎有问题..

做的时候:

df = pd.read_csv(r'path to file', sep=';')

我得到:

() 中的 OSError Traceback (最近一次调用最后一次) ----> 1 df = pd.read_csv(r'path Übersicht\Input\test\test.csv', sep=';')

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer,sep,delimiter,header,names,index_col,usecols,squeeze,prefix,mangle_dupe_cols,dtype,engine,converters, true_values,false_values,skipinitialspace,skirows,nrows,na_values,keep_default_na,na_filter,详细,skip_blank_lines,parse_dates,infer_datetime_format,keep_date_col,date_parser,dayfirst,迭代器,块大小,压缩,数千,十进制,lineterminator,quotechar,quoting,escapechar,comment,编码、方言、tupleize_cols、error_bad_lines、warn_bad_lines、skipfooter、skip_footer、双引号、delim_whitespace、as_recarray、compact_ints、use_unsigned、low_memory、buffer_lines、memory_map、float_precision) 703 skip_blank_lines=skip_blank_lines) 704 -->705 返回_read(filepath_or_buffer, kwds) 706 707 parser_f。名字=名字

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds) 443 444 # 创建解析器。--> 445 parser = TextFileReader(filepath_or_buffer, **kwds) 446 447 如果是块大小或迭代器:

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in init (self, f, engine, **kwds) 812 self.options['has_index_names'] = kwds['has_index_names'] 813 --> 814 self._make_engine(self.engine) 815 816 def close(self):

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine) 1043 def _make_engine(self, engine='c'): 1044 if engine == 'c': -> 1045 self._engine = CParserWrapper(self.f, **self.options) 1046 else: 1047 if engine == 'python':

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in init (self, src, **kwds) 1682 kwds['allow_leading_cols'] = self.index_col is not False 1683 -> 1684 self._reader = parsers.TextReader(src, **kwds) 1685 1686 # XXX

pandas._libs.parsers.TextReader 中的 pandas_libs\parsers.pyx。初始化()

pandas_libs\parsers.pyx 在 pandas._libs.parsers.TextReader._setup_parser_source()

OSError:从文件初始化失败

可以毫无问题地访问同一文件夹中属于 XLS 文件的其他文件。

像这样使用 Python 库时:

import csv
file = csv.reader(open(r'pathtofile')) 

for row in file:
    print(row)
    break

df = pd.read_csv(file, sep=';')

正在加载文件并打印第一行。但是我得到:

ValueError:文件路径或缓冲区对象类型无效:

可能是因为我不能用read_csv这种方式...

如何让第一个pandas功能发挥作用?csv 不包含除德语以外的任何特殊字符。文件大小为 10MB。

标签: pythonpandascsvdataframe

解决方案


import pandas as pd
pd.read_csv("your_file.txt", engine='python')

尝试这个。它完全适合我。


推荐阅读