Ошибка кодировки в python ahk
# -*- coding: utf-8 -*-
from ahk import AHK
control = AHK()
while True:
if control.key_state('CapsLock', mode='T'):
control.send_input("Привет мир!{ENTER}")
elif control.key_state('NumLock', mode='T'):
break
Вывод: Привет РјРёСЂ
Ссылка на модуль: https://pypi.org/project/ahk/
Если запускать в обычном .ahk файле, то все работает
F6::
SendInput, Привет мир!{ENTER}
Вывод: Привет мир!
Ответы (2 шт):
Автор решения: Nimod
→ Ссылка
Я уже нашел решение!
Оказывается проблема была в модуле ahk.
Надо было открыть файл script.py
"Корневая папка Python\Lib\site-packages\ahk\script.py"
и внести изменение в функцию _run_script
def _run_script(self, script_text, **kwargs):
blocking = kwargs.pop('blocking', True)
runargs = [self.executable_path, '/ErrorStdOut', '*']
decode = kwargs.pop('decode', False)
script_bytes = bytes(script_text, 'utf-8')
if blocking:
result = subprocess.run(runargs, input=script_bytes,
stderr=subprocess.PIPE, stdout=subprocess.PIPE, **kwargs)
if decode:
logger.debug('Stdout: %s', repr(result.stdout))
logger.debug('Stderr: %s', repr(result.stderr))
return result.stdout.decode()
else:
return result
else:
proc = subprocess.Popen(runargs, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
try:
proc.communicate(script_bytes, timeout=0)
except subprocess.TimeoutExpired:
pass # for now, this seems needed to avoid blocking and use stdin
return proc
на
def _run_script(self, script_text, **kwargs):
blocking = kwargs.pop('blocking', True)
runargs = [self.executable_path, '/ErrorStdOut', '*']
decode = kwargs.pop('decode', False)
script_bytes = bytes(script_text, 'cp1251')
if blocking:
result = subprocess.run(runargs, input=script_bytes,
stderr=subprocess.PIPE, stdout=subprocess.PIPE, **kwargs)
if decode:
logger.debug('Stdout: %s', repr(result.stdout))
logger.debug('Stderr: %s', repr(result.stderr))
return result.stdout.decode()
else:
return result
else:
proc = subprocess.Popen(runargs, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
try:
proc.communicate(script_bytes, timeout=0)
except subprocess.TimeoutExpired:
pass # for now, this seems needed to avoid blocking and use stdin
return proc
(заменить кодировку "utf-8" на "cp1251")
Автор решения: Pavel Durmanov
→ Ссылка
Фиксить нужно не библиотеку, а ваш текст.
In [15]: text = "Привет мир!"
In [16]: cp = text.encode().decode("cp1251")
Вот ваша строка:
In [17]: cp
Out[17]: 'Привет мир!'
Чтобы вернуть ее в нормаьный вид, ее нужно правильно декодировать:
In [18]: cp.encode("cp1251").decode("utf8")
Out[18]: 'Привет мир!'