Python - 如何解决 Heroku 上 JSON 的 UTF8 问题?
Python - How do i solve UTF8 issues with JSON on Heroku?
我有一个包含字典列表的 .txt,其中有一些文件名,有些包含 ä ö ü。我正在尝试使用此代码加载它:
with open('res/mp3s_stats.txt', 'r', encoding="utf-8") as f:
data = json.load(f)
但我得到这个错误:
File "C:\Users\laure\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\laure\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\laure\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 6184: invalid continuation byte
我该怎么做才能解决这个问题?我已经在每次写入或读取此文件时使用 encoding="utf-8" :/
我的直觉告诉我您的 txt 文件实际上不是 UTF-8 编码的。 (0XE4
是有效的 ISO-8859-15 字符 (ä),但不是有效的 UTF-8 字符。)
我有一个包含字典列表的 .txt,其中有一些文件名,有些包含 ä ö ü。我正在尝试使用此代码加载它:
with open('res/mp3s_stats.txt', 'r', encoding="utf-8") as f:
data = json.load(f)
但我得到这个错误:
File "C:\Users\laure\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\laure\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\laure\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 6184: invalid continuation byte
我该怎么做才能解决这个问题?我已经在每次写入或读取此文件时使用 encoding="utf-8" :/
我的直觉告诉我您的 txt 文件实际上不是 UTF-8 编码的。 (0XE4
是有效的 ISO-8859-15 字符 (ä),但不是有效的 UTF-8 字符。)