python - 葡萄牙语编码 ã、ê、ç、á

标签 python encoding

我有这个代码:

with open('dataset.csv', "w", encoding="utf-8") as myfile:
    print(datafromAPI.decode("utf-8"),file=myfile)

在通过 API 从数据服务器解码数据集后保存 .csv 文件,但编码未显示正确的字符。例如,ã 显示 ã、ê 显示 ê、ç 显示 ç、á 显示 á 等 我试图从 Notepad++ 的编码开始,并在我的python代码中使用utf-8、latin-1、ISO 8859-1等进行更改,但没有任何效果。

最佳答案

我将不得不进一步调查,但有可能 print 在前往文件的途中对您的字符串做了一些奇怪的事情。

既然您知道 datafromAPI 是一个 bytes 对象,请考虑尝试以下两种不同的方法:

[写入+文字模式]

with open('dataset.csv', "w", encoding="utf-8") as myfile:
    myfile.write(datafromAPI.decode("utf-8"))

[写入 +二进制模式]

with open('dataset.csv', "wb") as myfile:
    myfile.write(datafromAPI)

如果您使用支持源编码的编辑器打开它,最后一个版本肯定可以使用。

更新: 删除了我不确定是否可以使用的版本——它不会。

关于python - 葡萄牙语编码 ã、ê、ç、á,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52749834/

相关文章:

python - 使用 pytest-qt 测试 PyQt5 应用程序时如何显示 GUI?

python - Django 模板剥离空格?

python - 使用 Python 和诱变剂进行 De-mojibaking

Perl 下拉菜单和 Unicode

c# - 如何将二进制 blob 编码为 Unicode blob?

php - WhatsApp:如何解码 *.db.crypt 文件?

php - mysql文本中出现奇怪的字符

python - 等待具有多个并行作业的子进程结束

python - tkinter 中的组合框字体大小

python - 类型错误 : __init__() missing 1 required positional argument: 'units'