首页 > 解决方案 > UnicodeDecodeError:“ascii”编解码器无法解码位置 223 中的字节 0xc3:序数不在范围内(128)

问题描述

我正在尝试从 k8s 中容器内的命令行为 django 应用程序执行读取文件命令,但出现此错误。我可以在本地执行相同的命令,但不能在 k8s 内部执行。我的完整错误堆栈如下:

raceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/code/setup/management/commands/load.py", line 40, in handle
    raw = file.read()
  File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 223: ordinal not in range(128)

这是代码:

with codecs.open(filename, 'r', encoding='utf-8') as file:
            raw = file.read()
        data = json.loads(raw)

标签: pythondjangokubernetesascii

解决方案


推荐阅读