解决pandas读取csv文件出错的问题

Traceback (most recent call last):
    train = pd.read_csv('/csv/candy.csv', nrows=230, dtype=dtypes)
  File "D:\Python\Python37\Lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "D:\Python\Python37\Lib\site-packages\pandas\io\parsers\readers.py", line 586, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "D:\Python\Python37\Lib\site-packages\pandas\io\parsers\readers.py", line 482, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "D:\Python\Python37\Lib\site-packages\pandas\io\parsers\readers.py", line 811, in __init__
    self._engine = self._make_engine(self.engine)
  File "D:\Python\Python37\Lib\site-packages\pandas\io\parsers\readers.py", line 1040, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "D:\Python\Python37\Lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 51, in __init__
    self._open_handles(src, kwds)
  File "D:\Python\Python37\Lib\site-packages\pandas\io\parsers\base_parser.py", line 229, in _open_handles
    errors=kwds.get("encoding_errors", "strict"),
  File "D:\Python\Python37\Lib\site-packages\pandas\io\common.py", line 707, in get_handle
    newline="",
FileNotFoundError: [Errno 2] No such file or directory: '/csv/candy.csv'

Process finished with exit code 1

从报错信息上可以看出:是文件名或者路径名称导致的报错
解决办法:
读取文件的文件名必须英文字符或者拼音,此外路径上也必须是英文字符或者拼音,中文文件名则会出现上述问题。

猜你喜欢

转载自blog.csdn.net/q4326rgeqaadfd/article/details/129956807