alireza
alireza

Reputation: 1195

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position

When I use open and read syntax to open and read file in Python 3 and change files encoding, but this error happened. I want to convert a text with any encoding to UTF-8 and save it.

"sin3" has an unknown encoding,

fh= open(sin3, mode="r", encoding='utf8')
ss= fh.read()

File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 34: invalid continuation byte

I used codecs and got this error:

fh= codecs.open(sin3, mode="r", encoding='utf8')
ss= fh.read()

File "/usr/lib/python3.2/codecs.py", line 679, in read
return self.reader.read(size)
File "/usr/lib/python3.2/codecs.py", line 482, in read
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 34: invalid continuation byte

Upvotes: 4

Views: 17556

Answers (3)

Farnaz
Farnaz

Reputation: 94

Try this:

fh = codecs.open(sin3, "r",encoding='utf-8', errors='ignore')

Upvotes: 1

Raj Jadhav
Raj Jadhav

Reputation: 1

You can solve this problem by using Pandas library

import pandas as pd
data=pd.read_csv("C:\\Users\\akashkumar\\Downloads\\Customers.csv",encoding='latin1')

Upvotes: 0

Gil Baggio
Gil Baggio

Reputation: 13963

Try this:

  • Open the csv file in Sublime text editor.
  • Save the file in utf-8 format.
  • In sublime, Click File -> Save with encoding -> UTF-8

Then, you can read your file as usual:

I would recommend using Pandas.

In Pandas, you can read it by using:

import pandas as pd
data = pd.read_csv('file_name.csv', encoding='utf-8')

Upvotes: 1

Related Questions