【发布时间】:2022-01-26 11:27:03
【问题描述】:
我正在尝试在 python 中读取 csv,并不断收到以下错误。我在另一台计算机上尝试了以前使用过的其他 csv 文件,但没有出现问题,我也收到了相同的错误消息。我最近换了电脑,但同样奇怪的是,昨天我读取了保存在同一网络位置的不同 csv,没有任何问题。我不知道是什么原因造成的,但如果有人有任何想法,我希望能够加载我以前的文件。
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
Input In [17], in <module>
1 import pandas as pd
----> 3 df = pd.read_csv(r"C:\Users\nabecker\OneDrive - McDermott Will & Emery LLP\Documents\Parent Data for Analysis.csv")
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
305 if len(args) > num_allow_args:
306 warnings.warn(
307 msg.format(arguments=arguments),
308 FutureWarning,
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py:586, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, 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, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
571 kwds_defaults = _refine_defaults_read(
572 dialect,
573 delimiter,
(...)
582 defaults={"delimiter": ","},
583 )
584 kwds.update(kwds_defaults)
--> 586 return _read(filepath_or_buffer, kwds)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py:482, in _read(filepath_or_buffer, kwds)
479 _validate_names(kwds.get("names", None))
481 # Create the parser.
--> 482 parser = TextFileReader(filepath_or_buffer, **kwds)
484 if chunksize or iterator:
485 return parser
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py:811, in TextFileReader.__init__(self, f, engine, **kwds)
808 if "has_index_names" in kwds:
809 self.options["has_index_names"] = kwds["has_index_names"]
--> 811 self._engine = self._make_engine(self.engine)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py:1040, in TextFileReader._make_engine(self, engine)
1036 raise ValueError(
1037 f"Unknown engine: {engine} (valid options are {mapping.keys()})"
1038 )
1039 # error: Too many arguments for "ParserBase"
-> 1040 return mapping[engine](self.f, **self.options)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py:69, in CParserWrapper.__init__(self, src, **kwds)
67 kwds["dtype"] = ensure_dtype_objs(kwds.get("dtype", None))
68 try:
---> 69 self._reader = parsers.TextReader(self.handles.handle, **kwds)
70 except Exception:
71 self.handles.close()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\parsers.pyx:542, in pandas._libs.parsers.TextReader.__cinit__()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\parsers.pyx:642, in pandas._libs.parsers.TextReader._get_header()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\parsers.pyx:843, in pandas._libs.parsers.TextReader._tokenize_rows()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\parsers.pyx:1917, in pandas._libs.parsers.raise_parser_error()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 95538: invalid continuation byte
【问题讨论】:
-
您知道要加载的文件的编码吗?查看此问题以检查文件中的无效字节:stackoverflow.com/questions/29465612/…
-
不,其他帖子没有回答我的问题。我知道解决方法,但关键是我昨天没有遇到与现在给我错误的文件存储在完全相同位置的文件的问题。此外,我以前保存和使用的旧 csv 文件不再有效。我只是不知道这是计算机问题还是 Windows 问题、Python 或 Pandas 版本问题还是什么。
-
你还没有回答我的问题:你知道文件的编码吗?如果是这样,请将其传递给
pd.read_csv()。否则它将假定 UTF-8 可能不正确。 -
不,我不知道编码。它们是带有数字和字符串的标准 excel 文件,没有特殊字符或任何东西。一种有效,一种无效,它们本质上是同一文件的不同版本。另外,我一周前使用的其他 10 个 csv 文件也不再阅读。他们之前都没有打嗝,并且都存储在同一个文件夹中。我意识到我可以将它们保存为 csv UTC-8 并且它们会起作用,但我真的很好奇为什么我以前从未用完全相同的文件这样做。
-
我把它传给了 pd。 read_csv(r"filepath") 最初,但那是我收到我发布的错误消息的时候。
标签: python-3.x pandas