When the subprocess module retrieves the Windows systeminfo and decodes 'shift-jis', the environment sends an exception.
PC1 has an exception: PC2 has been fine in the past.
PC2 is fine
By the way, by changing the following parts of the source code, there will be no exceptions for PC1 and PC2.
textout=retout.decode('shift-jis') ->textout=retout.decode()
·Windows 10 Pro
·Vscode 1.72.0
·Python 3.9.10
PC1 and PC2 are both in the above environment
UnicodeDecodeError: 'shift_jis' codec can't decode byte 0x97 in position 2475: illegal multibyte sequence
import json
import subprocess
cmd_list = ['systeminfo', '/fo', 'LIST' ]
cmdreturn=subprocess.run(
cmd_list, check = True, stdout = subprocess.PIPE)
retcode = cmdreturn.returncode
retout = cmdreturn.stdout
# textout = retout.decode()
try:
textout=retout.decode('shift-jis')
except Exception as:
print(e)
raise
text_list = textout.splitlines()
param_dict=dict()
c_key=None
for text_line in text_list:
if not text_line.startswith(''):
if': 'in text_line:
key_word=text_line.split(':',maxsplit=1)
if key_word[1].startswith(''):
key_word[1] = key_word[1].lstrip()
param_dict.setdefault(key_word[0], key_word[1])
c_key=key_word[0]
else:
temp=text_line.lstrip()
if'\t'intemp:
temp=temp.split('\t')
temp='.join(temp)
ifc_key in param_dict:
c_data=param_dict [c_key]
if isinstance(c_data,list):
set_list=c_data
set_list.append(temp)
pass
else:
set_list = [c_data, temp]
param_dict [c_key] = set_list
else:
ret_dump=json.dumps(param_dict,indent=2,ensure_ascii=False)
print(ret_dump)
Of course, there are other things besides ShiftJIS/CP932 outside of the Japanese language environment, and there are also cases where Windows 10 and later have UTF-8/CP65001.
How about checking Python version, Windows code page settings, environment variables such as PYTHONIOENCODING and PYTHONUTF8?
© 2024 OneMinuteCode. All rights reserved.