You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pandas read_csv读取CSV文件时遇多重错误求助

Pandas读取CSV文件报错求助

我是大学数据分析方向的编程新手,正在学习使用Pandas。尝试读取指定CSV文件时遇到多个错误,现寻求帮助。

我的代码:

df = pd.read_csv(r"C:\Users\julius\OneDrive\UNIVERSITÄT\Master\3. Semester\Fallstudie BA\KFZ_BelegPrf.csv")
df

首次报错信息(Unicode解码错误):

UnicodeDecodeError                        Traceback (most recent call last)
Cell In[3], line 1
----> 1 df = pd.read_csv(r"C:\Users\juliu\Desktop\KFZ_BelegPrf.csv")
      2 df

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:912, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)
    899 kwds_defaults = _refine_defaults_read(
    900     dialect,
    901     delimiter,
   (...)
    908     dtype_backend=dtype_backend,
    909 )
    910 kwds.update(kwds_defaults)
--> 912 return _read(filepath_or_buffer, kwds)

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:577, in _read(filepath_or_buffer, kwds)
    574 _validate_names(kwds.get("names", None))
    576 # Create the parser.
--> 577 parser = TextFileReader(filepath_or_buffer, **kwds)
    579 if chunksize or iterator:
    580     return parser

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:1407, in TextFileReader.__init__(self, f, engine, **kwds)
   1404     self.options["has_index_names"] = kwds["has_index_names"]
   1406 self.handles: IOHandles | None = None
-> 1407 self._engine = self._make_engine(f, self.engine)

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:1679, in TextFileReader._make_engine(self, f, engine)
   1676     raise ValueError(msg)
   1678 try:
-> 1679     return mapping[engine](f, **self.options)
   1680 except Exception:
   1681     if self.handles is not None:

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\c_parser_wrapper.py:93, in CParserWrapper.__init__(self, src, **kwds)
     90 if kwds["dtype_backend"] == "pyarrow":
     91     # Fail here loudly instead of in cython after reading
     92     import_optional_dependency("pyarrow")
---> 93 self._reader = parsers.TextReader(src, **kwds)
     95 self.unnamed_cols = self._reader.unnamed_cols
     97 # error: Cannot determine type of 'names'

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\parsers.pyx:550, in pandas._libs.parsers.TextReader.__cinit__()

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\parsers.pyx:639, in pandas._libs.parsers.TextReader._get_header()

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\parsers.pyx:850, in pandas._libs.parsers.TextReader._tokenize_rows()

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\parsers.pyx:861, in pandas._libs.parsers.TextReader._check_tokenize_status()

File C:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\parsers.pyx:2021, in pandas._libs.parsers.raise_parser_error()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 6625: invalid continuation byte

我尝试将编码改为latin1后,又出现新的解析错误:

ParserError                               Traceback (most recent call last)
c:\Users\juliu\OneDrive\UNIVERSITÄT\Master\3. Semester\Fallstudie BA\Fallstudie BA WS2324.ipynb Cell 3 line 1
----> 1 df = pd.read_csv(r"C:\Users\juliu\OneDrive\UNIVERSITÄT\Master\3. Semester\Fallstudie BA\KFZ_BelegPrf.csv", encoding='latin1')

File c:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:912, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)
    899 kwds_defaults = _refine_defaults_read(
    900     dialect,
    901     delimiter,
   (...)
    908     dtype_backend=dtype_backend,
    909 )
    910 kwds.update(kwds_defaults)
--> 912 return _read(filepath_or_buffer, kwds)

File c:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:583, in _read(filepath_or_buffer, kwds)
    580     return parser
    582 with parser:
--> 583     return parser.read(nrows)

File c:\ProgramData\anaconda3\Lib\site-packages\pandas\io\parsers\readers.py:1704, in TextFileReader.read(self, nrows)
   1697 nrows = validate_integer("nrows", nrows)
   1698 try:
   1699     # error: "ParserBase" has no attribute "read"
   1700     (
...

File c:\ProgramData\anaconda3\Lib\site-packages\pandas\_libs\parsers.pyx:2029, in pandas._libs.parsers.raise_parser_error()

ParserError: Error tokenizing data. C error: Expected 8 fields in line 5, saw 9

内容的提问来源于stack exchange,提问作者Schmiddl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 00:24:55