How to solve UnicodeDecodeError when reading csv - python

I am trying to open a csv file with pandas but i get this error:
test_tweets = pd.read_csv(r"C:\Users\22587\Downloads\data\test_tweets.csv")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 75: invalid start byte

0xa0 is the non breaking space. You maybe copied your data from a website and there was such an invisible character

Related

python error : codec can't decode byte 0xe4 in position 2857: invalid continuation byte

I am reading a latex file using
with open(inputFileName, 'r', encoding="utf8") as inputFileHandle:
for lineInput in inputFileHandle:
It fails with lineInput showing "% Declare common style"
with the error
can't decode byte 0xe4 in position 2857: invalid continuation byte
I can not see any strange characters in the latex file. What is the byte 0xe4 and how can I identify it in the tex file?

Is there a way to encode a csv file to UTF-8 in pandas?

My code: data = pd.read_csv('Downloads/samplefile.csv',low_memory=False, encoding='utf-8')
I receive the error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd1 in position 258663: invalid continuation byte
Any help is appreciated.
Your data file might NOT be encoded in UTF-8, because the character 0xd1 is Ñ in the encoding ISO8859-1.
So, use the line below:
data = pd.read_csv('Downloads/samplefile.csv',low_memory=False, encoding='iso8859-1')

Unable to Read a tsv file in pandas. Gives UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 113: invalid start byte

I am trying to read a tsv file (one of many) but it is given me the following error.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 113: invalid start byte
Before this I have read similar other files using the same code.
df = pd.read_csv(fname,sep='\t',error_bad_lines=False)
But for this particular, I am getting an error.
How can I read this file? Hopefully without missing any data.
A suggestion would be to check waht encoding you actually have. Do it this way:
with open('filename.tsv) as f: ### or whatever your etension is
print(f)
from that you'll obtain the encoding. Then,
df=pd.read_csv('filename.tsv', encoding="the encoding that was returned")
It would be nice if you could post lien 113 of you dataset, where the first occurrence of the error occured.

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

Basically, I was using pandas to read csv files to separate a column which had "Date + Hour" in the format "dd/mm/yy hh".
I had help here trying to write a script to separate the column in 2 different columns.
First of all, this is what the dataset looked like:
The joint field is "FECHA" and I managed to run this code on some of the csv files:
import pandas as pd,os
sal = pd.read_csv('C:/Users/drivasti/Documents/002_Script_Separa_Fecha_Hora/Anexo2_THP_UL.csv')
df=sal.join(sal['FECHA'].str.partition(' ')[[0, 2]]).rename({0: 'DATE', 2: 'HOUR'}, axis=1)
df.to_csv('C:/Users/drivasti/Documents/002_Script_Separa_Fecha_Hora/Anexo2_THP_UL_2.csv',index=False)
And they worked perfectly as seen here:
However, I encountered this error when I tried running another csv file (note that I change the name of the file everytime I have to run it, but they're all csv files):
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd1 in position 2: invalid continuation byte
Now I have tried some of the answers here but none have helped:
UnicodeDecodeError: 'utf-8' codec can't decode byte
'utf-8' codec can't decode byte 0xdb in position 1:
Anyone might know how to parse this as UTF-8? or is it a problem in the field "FECHA"?

I get char error while trying to pars CSV file into pandas

I am practicing pandas and i have next issue:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position
7190: invalid start byte
So its simple tryout of csv readout:
csvfile = open('file.csv', 'r', encoding="UTF-8")
csv_pandas = pd.read_csv(csvfile, sep=",")
print(csv_pandas)
However it works properly with csv module. With csv.reader i dont get same error.
Whats going on? And where can i learn more about charmap and encodings with python?
p.s. I tried out by removing encoding="UTF-8" and i got similar error:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position
140378: character maps to

Categories

Resources