首页 > 解决方案 > UnicodeDecodeError:“utf-8”编解码器无法解码位置 76 中的字节 0x81:无效的起始字节

问题描述

我想创建一个解码 wifi 密码的程序。我还没有完成,但程序应该打印所有 wifi 的名称。但是有这个错误,我不知道如何解决它。

import subprocess

data = subprocess.check_output(["netsh", "wlan", "show", "profiles"]).decode("utf-8").split("\n")
wifis = [line.split(":")(1)[1:-1] for line in data if "All User Profile" in line]
print (data)

错误:

data = subprocess.check_output(["netsh", "wlan", "show", "profiles"]).decode("utf-8").split()
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 76: invalid start byte

标签: python

解决方案


netsh提示您使用的是 Windows 系统。控制台应用程序通常对西欧语言使用 cp850 编码。所以你可以尝试:

data = subprocess.check_output(["netsh", "wlan", "show", "profiles"]).decode("cp850").split("\n")

或者为了安全起见,您可以使用能够接受任何输入字节(如 latin1)的编码,但它很少在 Windows 上返回预期的字符。但是当输入不是 utf-8 编码时,切勿使用。utf-8


推荐阅读