Python Unicodedecodeerro in jupyter notebook. Working with . csv

Asked

Viewed 894 times

3

I’m learning to manipulate files .csv in Python with the jupyter notebook.

I extracted a file from the system, in csv, separated by commas, but when I opened I received this error:

arqsb2 = open('arquivos/sb2.csv','r')

sb2 = arqsb2.read()


    ---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-2-084f7ffc9ea2> in <module>()
----> 1 sb2 = arqsb2.read()

C:\ProgramData\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 1022110: character maps to <undefined>

Could someone help me?

  • Rafael, in the file opening method, tries to pass the encoding already, something like: arqsb2 = open('arquivos/sb2.csv',encoding='utf-8','r'), I fished it out of doc

1 answer

0

Rafael you can open the document as follows:

open('arquivos/sb2.csv',encoding='utf-8','r')

If you keep giving this error look at the encoding of your document. Another very interesting way to work with csv, alias the best lib of all and use pandas:

import pandas as pd
data = pd.read_csv('caminho/do/documento.csv')

The pandas and extremely powerful to handle documents type csv and you will have a complete tool of definitive.

  • did not work -> File "<ipython-input-1-d5e9fe0d14a7>", line 1 y = open('files/stock.csv', encoding='utf-8', 'r') Syntaxerror: positional argument Follows keyword argument

  • 1

    y = open('files/stock.csv', encoding='utf-8', 'r')

  • 1

    File "<ipython-input-1-d5e9fe0d14a7>", line 1 y = open('files/stock.csv', encoding='utf-8', 'r') Syntaxerror: positional argument Follows keyword argument

  • This is a very common error. The function takes two types of key and value arguments and the positional one. In this case the positional arguments come before. Then try. Y= open ('files/stock.csv','r', encoding='utf-8')

  • y = open("stock.csv", 'r', encoding='utf-8')

  • Unicodedecodeerror Traceback (Most recent call last) <ipython-input-6-cd696e05bb90> in <module>() -----> 1 print(y.read()) ~ Anaconda3 lib codecs.py in Decode(self, input, final) 319 # Decode input (taking the buffer into Account) 320 data = self.buffer + input -> 321 (result, Consumed) = self. _buffer_decode(data, self.errors, final) 322 # Keep undecoded input until the next call 323 self.buffer = data[Consumed:] Unicodedecodeerror: 'utf-8' codec can’t Decode byte 0xe7 in position 28: invalid continuation byte

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.