python - 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
解决方案
netsh
提示您使用的是 Windows 系统。控制台应用程序通常对西欧语言使用 cp850 编码。所以你可以尝试:
data = subprocess.check_output(["netsh", "wlan", "show", "profiles"]).decode("cp850").split("\n")
或者为了安全起见,您可以使用能够接受任何输入字节(如 latin1)的编码,但它很少在 Windows 上返回预期的字符。但是当输入不是 utf-8 编码时,切勿使用。utf-8
推荐阅读
- javascript - 反应(表情符号)收集并获得确切的数字
- python - 为什么这个网站的请求模块随机失败
- python - Pollard 的 p-1 算法的第二阶段
- javascript - 多次执行“clearInterval”时计时器不停止
- typescript - TypeScript 声明文件(函数构造函数)
- matlab - 用插值和回归绘制 8 次多项式
- android - Android Emulator - 拉取文件时权限被拒绝
- php - 将文本文件加载到 mysql 数据库中并为每一行添加时间戳
- python - 在Python中将数字日期转换回日期
- regex - Vim Regex:我如何匹配文字“大于”(“>”)/“小于”(“<”)符号?