【发布时间】:2018-10-24 20:00:45
【问题描述】:
我使用以下代码读取 CSV 文件:
address=r'C:\Users\ssadangi\Desktop\Lynda Python data analytics\Ch02\02_05\Superstore-Sales.csv'
df=pd.read_csv(address,index_col='Order Date',parse_dates=True)
代码给了我这个错误:
UnicodeDecodeError Traceback (most recent call last) pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._string_convert() pandas/_libs/parsers.pyx in pandas._libs.parsers._string_box_utf8() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 16: invalid start byte
在处理上述异常的过程中,又发生了一个异常:
UnicodeDecodeError Traceback (most recent call last) <ipython-input-28-3fa20db347ab> in <module>() ----> 1 df=pd.read_csv('Superstore-Sales.csv',index_col='Order Date',parse_dates=True) ~\Documents\Softwares\Anaconda\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) 707 skip_blank_lines=skip_blank_lines) 708 --> 709 return _read(filepath_or_buffer, kwds) 710 711 parser_f.__name__ = name ~\Documents\Softwares\Anaconda\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds) 453 454 try: --> 455 data = parser.read(nrows) 456 finally: 457 parser.close() ~\Documents\Softwares\Anaconda\lib\site-packages\pandas\io\parsers.py in read(self, nrows) 1067 raise ValueError('skipfooter not supported for iteration') 1068 -> 1069 ret = self._engine.read(nrows) 1070 1071 if self.options.get('as_recarray'): ~\Documents\Softwares\Anaconda\lib\site-packages\pandas\io\parsers.py in read(self, nrows) 1837 def read(self, nrows=None): 1838 try: -> 1839 data = self._reader.read(nrows) 1840 except StopIteration: 1841 if self._first_chunk: pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_column_data() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._string_convert() pandas/_libs/parsers.pyx in pandas._libs.parsers._string_box_utf8() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 16: invalid start byte
【问题讨论】:
-
没有原始数据,没有多少人可以告诉你。
-
欢迎来到 Stack Overflow!我已经使用格式代码编辑了您的问题以提高可读性(有关格式的更多信息,请参阅the editing help)。我还尝试让标题更友好,并添加了 [python] 标签——您应该始终使用基本语言标签。
-
考虑编辑您的问题并添加导致错误的 CSV 文件部分 - 尝试删除部分 CSV 文件以使其尽可能短但仍会导致错误,然后向我们展示.
-
Tre 设置编码 例如:
encoding='latin1'或encoding='iso-8859-1' -
感谢您编辑问题@Cris Luengo。这是我在 Stack Overflow 的第一次。我尝试使用格式化代码,但似乎不起作用(甚至标记似乎也不起作用)。网络或浏览器的一些问题。通过设置
encoding='ansi'解决。谢谢@Rakesh
标签: python python-3.x csv parsing python-unicode