pandas 无法读取csv文件在jupyter笔记本和以下错误来

lkaoscv7  于 2023-02-11  发布在  其他
关注(0)|答案(2)|浏览(288)

import pandas as pd df = pd.read_csv('D:\Tableau\codebasics_files\Weather_data.csv.xlsx') df ​

UnicodeDecodeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_18872\1985582496.py in 1 import pandas as pd ----> 2 df = pd.read_csv('D:\Tableau\codebasics_files\Weather_data.csv.xlsx') 3 df
C:\ProgramData\Anaconda3\lib\site-packages\pandas\util_decorators.py in wrapper(*args, **kwargs) 309 stacklevel=stacklevel, 310 ) --> 311 return func(*args, **kwargs) 312 313 return wrapper
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py 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) 676 kwds.update(kwds_defaults) 677 --> 678 return _read(filepath_or_buffer, kwds) 679 680
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py in _read(filepath_or_buffer, kwds) 573 574 # Create the parser. --> 575 parser = TextFileReader(filepath_or_buffer, **kwds) 576 577 if chunksize or iterator:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py in init(self, f, engine, **kwds) 930 931 self.handles: IOHandles | None = None --> 932 self._engine = self._make_engine(f, self.engine) 933 934 def close(self):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py in _make_engine(self, f, engine) 1232 1233 try: -> 1234 return mapping[engine](f, **self.options) 1235 except Exception: 1236 if self.handles is not None:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py in init(self, src, **kwds) 73 74 kwds["dtype"] = ensure_dtype_objs(kwds.get("dtype", None)) ---> 75 self._reader = parsers.TextReader(src, **kwds) 76 77 self.unnamed_cols = self._reader.unnamed_cols
C:\ProgramData\Anaconda3\lib\site-packages\pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader.cinit()
C:\ProgramData\Anaconda3\lib\site-packages\pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader._get_header()
C:\ProgramData\Anaconda3\lib\site-packages\pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()
C:\ProgramData\Anaconda3\lib\site-packages\pandas_libs\parsers.pyx in pandas._libs.parsers.raise_parser_error()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 14: invalid start byte

i tried some options using youtube but not working

0vvn1miw

0vvn1miw1#

查看文件扩展名的最末尾,您导入的是.xlsx文件,而不是CSV文件。
尝试在Excel中打开该文件并将其导出为CSV。您需要确保.csv是该文件的最后一个字符。

e0bqpujr

e0bqpujr2#

我认为你需要使用XLSX函数来加载和读取Pandas里面的XLSX文件。
为此,您需要在代码中使用以下代码行:

import pandas as pd
df = pd.read_excel('D:\Tableau\codebasics_files\Weather_data.csv.xlsx')

相关问题