Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-如何在Heroku上使用JSON解决UTF8问题?_Python_Json_Utf - Fatal编程技术网

Python-如何在Heroku上使用JSON解决UTF8问题?

Python-如何在Heroku上使用JSON解决UTF8问题?,python,json,utf,Python,Json,Utf,我有一个.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\comm

我有一个.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字符。)

打开文件时,不指定编码,因此Python使用默认编码;显然,与heroku相比,您的机器上的默认设置有所不同。我倾向于指定“utf-8”作为编码(因为它可以编码任何unicode字符),并确保json文件保存为utf-8。哦,这是我没有考虑的事情,在记事本中保存文件时,我更改了编码,它成功了,谢谢!