UTF-8 的问题。重音打印错误
Problem with UTF-8. The accent is printing wrong
我是 Python(3.9 版)的新手,我正在尝试读取和打印 CSV 文件,但是,在打印时,居中显示有误。我用过 'UTF-8'、'latin' 和 'ISO-8859-1'。但是 none 起作用了。
我的代码:
import io
import csv
with io.open(filename.csv, 'r', encoding='utf-8') as f:
text = f.read()
# process Unicode text
with io.open(filename.csv, 'w', encoding='utf-8') as f:
f.write(text)
print(text.encode('utf-8'))
打印:
b'\xef\xbb\xbf"N\xc3\xbamero,""Descri\xc3\xa7\xc3\xa3o"",""Fonte"",""Situa\xc3\xa7\xc3\xa3o""
我该如何解决这个问题?
显然,输入文件包含 Byte order mark b'\xef\xbb\xbf'
. Apply utf_8_sig
— UTF-8 codec with BOM signature(将跳过数据开头的可选 UTF-8 编码 BOM)。
import io
import csv
with io.open(filename.csv, 'r', encoding='utf_8_sig') as f:
text = f.read()
# process Unicode text
with io.open(filename.csv, 'w', encoding='utf-8') as f:
f.write(text)
print(text)
# "Número","Descrição","Fonte","Situação"
不确定 return 值,因为您的输入和输出示例不是 minimal, complete and verifiable
我将打印 (text.encode (utf-8))
更改为 print(text)
并且有效。
我是 Python(3.9 版)的新手,我正在尝试读取和打印 CSV 文件,但是,在打印时,居中显示有误。我用过 'UTF-8'、'latin' 和 'ISO-8859-1'。但是 none 起作用了。
我的代码:
import io
import csv
with io.open(filename.csv, 'r', encoding='utf-8') as f:
text = f.read()
# process Unicode text
with io.open(filename.csv, 'w', encoding='utf-8') as f:
f.write(text)
print(text.encode('utf-8'))
打印:
b'\xef\xbb\xbf"N\xc3\xbamero,""Descri\xc3\xa7\xc3\xa3o"",""Fonte"",""Situa\xc3\xa7\xc3\xa3o""
我该如何解决这个问题?
显然,输入文件包含 Byte order mark b'\xef\xbb\xbf'
. Apply utf_8_sig
— UTF-8 codec with BOM signature(将跳过数据开头的可选 UTF-8 编码 BOM)。
import io
import csv
with io.open(filename.csv, 'r', encoding='utf_8_sig') as f:
text = f.read()
# process Unicode text
with io.open(filename.csv, 'w', encoding='utf-8') as f:
f.write(text)
print(text)
# "Número","Descrição","Fonte","Situação"
不确定 return 值,因为您的输入和输出示例不是 minimal, complete and verifiable
我将打印 (text.encode (utf-8))
更改为 print(text)
并且有效。