OSError:如何解决从文件中初始化Pandas中的csv失败?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (960)

到目前为止,panda阅读我的所有CSV文件没有任何问题。

在做时:

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

我得到:

OSError Traceback (most recent call last) in () ----> 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, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, 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 return _read(filepath_or_buffer, kwds) 706 707 parser_f.name = name

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds) 443 444 # Create the parser. --> 445 parser = TextFileReader(filepath_or_buffer, **kwds) 446 447 if chunksize or iterator:

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.pyx in pandas._libs.parsers.TextReader.cinit()

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

OSError: Initializing from file failed

与XLS文件相同的文件夹中的其他文件可以毫无问题地被访问。

当像这样使用Python库时:

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

for row in file:
    print(row)
    break

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

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

ValueError: Invalid file path or buffer object type:

可能是因为我不能以这种方式使用read_csv 。

提问于
用户回答回答于

如果你只想读取csv文件,并获得将在控制台中显示为文本的结果,请执行此操作:

import csv
with open('your_file.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        print(row)
csvFile.close()

注意:代码是针对Python 3的,如果使用Python 2,则打印语法不使用括号。

扫码关注云+社区

领取腾讯云代金券