【问题标题】:How to fix UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 5: invalid continuation byte如何修复 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 5: invalid continuation byte
【发布时间】:2020-03-01 19:05:59
【问题描述】:

我试图在服务器上运行一个新创建的环境并得到 UnicodeDecodeError。

Windows 7,64 位。我创建了一个虚拟环境并将代码定向到源文件夹。这里安装了 django、python 和 virtualenv 版本。我尝试安装旧版本的 django 和 python 并得到同样的错误。

在搜索故障排除建议时,我只找到与 .csv 和 pandas 相关的文章。

Django==2.2.7
pytz==2019.3
virtualenv==16.7.7
PS: C:\Users\User\Dev\Selectia\src> python manage.py runserver

这是输出:

Watching for file changes with StartReloader
Performing system checks...

System checkidentified no issues (0 silenced).
November 05, 2019 - 12:02:31
Django version 2.2.7, using setings 'foodie.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREALK.
Exception in thread django-main-thread:
Traceback (most recent call last):
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
   self.run()
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
  self._target(*self.args, **self._kwargs)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 54 in wrapper 
  fn(*args, **kwargs)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 203, in run
  httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 67, in __init__
  super().__init__(*args, **kwargs)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 452, in __init__
  self.server_bind()
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\wsgiref\simple_server.py", line 50, in server_bind
  HTTPServer.server_bind(self)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 139, in server_bind
  self.server_name = socket.getfqdn(host)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socket.py", line 676, in getfqdn
  hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecoderError: 'utf-8' code can't decode byte 0xcf in position 5: invalid continuation byte

您能帮我找出解决此错误消息的方法吗?

附:这是我的第一个 web 框架项目,我对 python 有一些了解。我非常感谢提供的任何帮助。我想建立自己的网站。

【问题讨论】:

标签: python django unicode utf-8 decoder


【解决方案1】:

您的主机文件中有别名,其中包含无法解码为 UTF-8 的字符。删除它们以解决问题(将任何别名设为仅 ASCII 可能是最简单的)。

Windows 7 上主机文件的默认位置是

 C:\Windows\System32\Drivers\etc\hosts

【讨论】:

  • 这个issue 应该是在 Python 3.5 中修复的,所以知道你的 hosts 文件中有什么会很有趣。
  • 我已经到上面提到的文件夹并检查了来自 UTF-8 验证器的所有文件。所有文件都经过了很好的验证。有没有办法可以与您共享 hosts 文件夹中的文件?
  • 如果主机文件不是很大(并且不包含任何敏感信息),您可以将内容粘贴到您的问题中。 open(r'C:\Windows\System32\Drivers\etc\hosts', 'rb').read() 的输出会特别有用,因为它会显示实际字节数。
  • 我遇到了同样的错误,我的主机文件完全正常。它具有默认内容和三行,即 ascii - 127。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2020-12-26
  • 2021-12-01
相关资源
最近更新 更多