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.
当我使用open和read语法在python3中打开和读取文件并改变文件编码时,这个错误发生了。我想把一个文本转换成UTF-8编码并保存它。
"sin3" has an unknown encoding,
“sin3”有一个未知的编码,
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文件“/ usr / lib / python3.2 /编解码器。在decode(结果,被消耗)= self中,第300行。_buffer_decode(数据、自我。错误,最终)UnicodeDecodeError: 'utf-8' codec不能解码第34位的字节0xc7:无效的延续字节。
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文件“/ usr / lib / python3.2 /编解码器。读取(大小)文件/usr/lib/python3.2/编解码器。在读取的newchars中,第482行,decodedbytes = self。decode(data, self。errors) UnicodeDecodeError: 'utf-8' codec不能解码第34位的字节0xc7:无效的连续字节。
1 个解决方案
#1
0
Try this:
试试这个:
- Open the csv file in Sublime text editor.
- 在卓越的文本编辑器中打开csv文件。
- Save the file in utf-8 format.
- 以utf-8格式保存文件。
- In sublime, Click File -> Save with encoding -> UTF-8
- 在崇高,点击文件->保存与编码-> 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')
#1
0
Try this:
试试这个:
- Open the csv file in Sublime text editor.
- 在卓越的文本编辑器中打开csv文件。
- Save the file in utf-8 format.
- 以utf-8格式保存文件。
- In sublime, Click File -> Save with encoding -> UTF-8
- 在崇高,点击文件->保存与编码-> 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')