Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro ao importar CSV no notebook

Estou usando o Jupyter Notebook através do VS Code para importar uma base em csv da seguinte forma

ceaps_2019 = pd.read_csv('C:/Users/carlo/OneDrive/Arquivos/Alura/7DoC_Dados/1/despesa_ceaps_2019.csv',encoding="utf-8", sep=';')
ceaps_2019

Usei o encoding para tratar o erro mas ele persiste mesmo assim

UnicodeDecodeError                        Traceback (most recent call last)
File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1088, in pandas._libs.parsers.TextReader._convert_tokens()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1238, in pandas._libs.parsers.TextReader._convert_with_dtype()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1251, in pandas._libs.parsers.TextReader._string_convert()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1499, in pandas._libs.parsers._string_box_utf8()

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

During handling of the above exception, another exception occurred:

UnicodeDecodeError                        Traceback (most recent call last)
Cell In[22], line 1
----> 1 ceaps_2019 = pd.read_csv('C:/Users/carlo/OneDrive/Arquivos/Alura/7DoC_Dados/1/despesa_ceaps_2019.csv',encoding="utf-8", sep=";")
      2 ceaps_2019

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\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:\Users\carlo\AppData\Local\Programs\Python\Python311\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:\Users\carlo\AppData\Local\Programs\Python\Python311\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     (
   1701         index,
   1702         columns,
   1703         col_dict,
-> 1704     ) = self._engine.read(  # type: ignore[attr-defined]
   1705         nrows
   1706     )
   1707 except Exception:
   1708     self.close()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\parsers\c_parser_wrapper.py:234, in CParserWrapper.read(self, nrows)
    232 try:
    233     if self.low_memory:
--> 234         chunks = self._reader.read_low_memory(nrows)
    235         # destructive to chunks
    236         data = _concatenate_chunks(chunks)

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:812, in pandas._libs.parsers.TextReader.read_low_memory()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:889, in pandas._libs.parsers.TextReader._read_rows()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1034, in pandas._libs.parsers.TextReader._convert_column_data()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1095, in pandas._libs.parsers.TextReader._convert_tokens()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1238, in pandas._libs.parsers.TextReader._convert_with_dtype()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1251, in pandas._libs.parsers.TextReader._string_convert()

File c:\Users\carlo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\_libs\parsers.pyx:1499, in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 1: invalid continuation byte
2 respostas
solução!

Boa noite, Carlos. Tudo bem?

Removendo o encoding="utf-8" funciona ?

Para teste, adiciona o arquivo em uma pasta de caminho mais curto, de repente fica mais fácil de achar o erro.

Obrigado Amanda, eu tinha feito sem o código também mas não solucionava o problema. Acabou que era o tamanho do caminho mesmo, odeio esse problema pq sempre mexe com minha organização dos arquivos.