簡體   English   中英

UnicodeDecodeError: 'utf-8' 編解碼器無法解碼位置 1551 中的字節 0x87:起始字節無效

[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 1551: invalid start byte

我正在嘗試在 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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM