简体   繁体   English

Python3.7 和计划任务的 Unicode 问题

[英]Unicode issue with Python3.7 and Scheduled Tasks

I am trying to collect the names of the Scheduled tasks in Python using subprocess我正在尝试使用子进程收集 Python 中的计划任务的名称

import subprocess
import sys

encoding = 'utf-8'

cmd = r'''$env:PYTHONIOENCODING = "%s";py -3 -c "print('® ¾ ü_ä_ö')"'''% encoding
#cmd = r'''$env:PYTHONIOENCODING = "%s"; schtasks /query ''' % encoding

data = subprocess.check_output(["powershell", "-C",cmd])
print((data.decode(encoding)))

This works fine when I do the dummy cmd (print the Unicode).当我执行虚拟 cmd(打印 Unicode)时,这工作正常。 But when I try to run the schtasks command (some task like intel and others uses unicode symbols like ® in the task name or characters like ü_ä_ö ).但是当我尝试运行 schtasks 命令时(一些任务,如 intel 和其他任务使用 unicode 符号,如任务名称中的 ® 或类似 ü_ä_ö 的字符)。

This gives me the following error:这给了我以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 1228: invalid start byte UnicodeDecodeError:“utf-8”编解码器无法解码 position 1228 中的字节 0x81:无效的起始字节

If I run the command from cmd prompt or powershell directly it shows fine:如果我从 cmd 提示符或 powershell 直接运行命令,则显示正常:

C:\Users\ricar\Google Drive\Bifrost\Collectors>schtasks /query

Folder: \
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
Adobe Acrobat Update Task                12/4/2020 8:00:00 AM   Ready
AdobeAAMüpdater-1.0-MicrosoftAccount-ric 12/4/2020 2:00:00 AM   Ready
AdobeGCInvoker-1.0                       12/5/2020 12:30:00 AM  Ready
HPPSDrTelemetryWatch©                    12/12/2020 12:00:00 AM Ready
Intel-IMSS®                              N/A                    Ready

Any ideas what I am doing wrong?任何想法我做错了什么?

Thanks谢谢

Are you sure the schtasks output is in utf-8?您确定schtasks output 在 utf-8 中吗?

0x81 is ü in the IBM CP437 and IBM CP850 / IBM CP858 encodings. 0x81 在 IBM CP437 和 IBM CP850 / IBM CP858 编码中是 ü。

In order to check this, the pragmatic way is to print out the string with repr() or with one of the decode(encoding, errors=...) options that outputs character codes (eg. decode(encoding, errors='xmlcharrefreplace') ), then match it up with tables of encodings to see which one matches.为了检查这一点,实用的方法是使用repr()或使用输出字符代码的decode(encoding, errors=...)选项之一打印出字符串(例如decode(encoding, errors='xmlcharrefreplace') ),然后将其与编码表匹配以查看哪个匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM