python - UnicodeDecodeError:“utf-8”编解码器无法解码位置 1551 中的字节 0x87:无效的起始字节
问题描述
我正在尝试在 Python 上检查 Wifi 密码程序,所以我遇到了一些关于 unicodeDecodeError 的问题,我无法理解
这是我的更新终端:
C:\Users\bartu\Desktop\Python\pythonSmallProject>python check_wifi_password.py
Traceback (most recent call last):
File "check_wifi_password.py", line 10, in <module>
data = subprocess.check_output(['netsh','wlan','show','profiles']).decode(get_encoding).split('\n') # now we will store the profiles data in the "data" variable by
TypeError: decode() argument 'encoding' must be str, not function
这是我的更新代码:
import subprocess # first we will import the subprocess module
import os
from chardet import detect
def get_encoding(file):
with open(file,'rb') as f:
data = f.read()
return detect(data)['encoding']
data = subprocess.check_output(['netsh','wlan','show','profiles']).decode(get_encoding).split('\n') # now we will store the profiles data in the "data" variable by
# running the 1st cmd command using subprocess.check_output
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i] # now we will store the profile by converting them to list
# using for loop in python we re checking and printing the Wifi
# passwords if they are avaiable using the 2nd cmd command
for i in profiles:
# running the 2nd cmd command to check passwords
results = subprocess.check_output(['netsh','wlan','show','profile',i,'key = clear']).decode(get_encoding).split('\n')
# storing passwords after converting them to list
results = [j.split(":")[1][1:-1] for j in results if " Key Content" in j]
# printing the profiles(wifi name) with their passwords using
# try and except
try:
print("{:<30} | {:<}".format(i,results[0]))
except IndexError:
print("{:<30} | {:<}".format(i, ""))
except subprocess.CalledProcessError:
print("{:<30} | {:<}".format(i, "ENCODING ERROR"))
print("")
解决方案
您是否尝试过使用latin
编码?
data = subprocess.check_output(['netsh','wlan','show','profiles']).decode('latin1').split('\n')
如果它不起作用,请尝试从文件中获取编码 -
import os
from chardet import detect
def get_encoding(data):
return detect(data)['encoding']
data = subprocess.check_output(['netsh','wlan','show','profiles'])
data_encoding = get_encoding(data)
decoded_data = data.decode(data_encoding).split('\n')
推荐阅读
- c++ - 有条件地指定 noexcept 函数
- flutter - 如何根据flutter中的动态时区当前时间获取时间
- python - 引发错误.DNSLookupError(twisted.internet.error.DNSLookupError:DNS 查找失败:主机名查找没有结果:x
- javascript - Autocomplete.js 粘贴不显示我的数据列表
- javascript - JQuery数据表事件修改按钮
- java - 无法从 Servlet 检索数据
- javascript - 我对实习生的测试任务。Javascript - 二次方程和博弈论
- next.js - 运行 npm run dev 时下一个 js 创建下一个应用程序错误?
- php - sqlite (php) 优化:一次查询多个单词,但保持结果按发送顺序排列
- c++ - 如何在 Visual Studio 2019 中使用 CppFlow 库?