【问题标题】:Python str.translate unicodePython str.translate unicode
【发布时间】:2018-09-02 19:30:42
【问题描述】:

所以我正在创建一个 python 键盘记录器。当按下一个键时,我设法获得了不同的修饰键状态,因此我开发了一个功能,例如在按下 shift 时将字母放在大写中,这里是:

def applyKeyModifier(x,shift,ctrl,altgr,win):
    keyboard      = u"""²&é"'(-è_çà)=azertyuiop^$qsdfghjklmù*<wxcvbn,;:!/*-+"""
    shiftModified = u"""_1234567890°+AZERTYUIOP¨£QSDFGHJKLM%µ>WXCVBN?./§/*-+"""
    altgrModified = u"""__~#{[|`\^@]}€__________¤___________________________"""

    shiftTranslation = string.maketrans(keyboard, shiftModified)
    altgrTranslation = string.maketrans(keyboard, altgrModified)
    if shift and not altgr and not ctrl and not win:
        translated = x.translate(shiftTranslation)
        if translated == "_":
            translated=""
        return translated
    elif altgr and not shift and not ctrl and not win:
        translated = x.translate(shiftTranslation)
        if translated == "_":
            translated=""
        return translated
    elif ctrl and not shift and not altgr and not win: 
        return " [CTRL+"+x+"] "
    elif win and not shift and not altgr and not ctrl: 
        return " [WIN/CMD+"+x+"] "
    else:
        return x

唯一的问题是我得到了这个错误:

C:\Users\tugle\Desktop>python keylogger.py
Traceback (most recent call last):
  File "keylogger.py", line 139, in <module>
    listener.join()
  File "C:\dev\Python27\lib\site-packages\pynput\_util\__init__.py", line 199, in join
    six.reraise(exc_type, exc_value, exc_traceback)
  File "C:\dev\Python27\lib\site-packages\pynput\_util\__init__.py", line 154, in inner
    return f(self, *args, **kwargs)
  File "C:\dev\Python27\lib\site-packages\pynput\keyboard\_win32.py", line 237, in _process
    self.on_press(key)
  File "C:\dev\Python27\lib\site-packages\pynput\_util\__init__.py", line 75, in inner
    if f(*args) is False:
  File "keylogger.py", line 117, in on_press
    keybuffer += applyKeyModifier(str(key),isShift,isCtrl,isAltGr,isWin)
  File "keylogger.py", line 19, in applyKeyModifier
    shiftTranslation = string.maketrans(keyboard, shiftModified)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb2' in position 0: ordinal not in range(128)

我正在使用 python2。那么有人可以在这里帮助我吗

【问题讨论】:

    标签: python unicode translation python-2.x


    【解决方案1】:

    translate() 方法的行为不同,具体取决于它是在 str 还是 unicode 上调用的。您正在使用非 ASCII 字符,因此您的字符串应该是 unicode 对象,并且 unicode.translate() 采用映射 (dict) 而不是 maketrans 表。 Quoth the docs:

    对于 Unicode 对象,translate() 方法不接受可选的 deletechars 参数。相反,它返回 s 的副本,其中所有字符都已通过给定的转换表进行映射,该转换表必须是 Unicode 序数到 Unicode 序数、Unicode 字符串或None 的映射。未映射的字符保持不变。映射到None 的字符被删除。

    因此,shiftTranslation 需要采用以下形式:

    shiftTranslation = {
        ord(u'²'): u'_',
        ord(u'&'): u'1',
        ord(u'é'): u'2',
        # etc.
    }
    

    【讨论】:

    • 我现在有:TypeError: expected a string or other character buffer object
    猜你喜欢
    • 2015-09-17
    • 2010-11-22
    • 1970-01-01
    • 2014-07-28
    • 2013-10-25
    • 2016-03-21
    • 2017-06-17
    • 2023-03-07
    相关资源
    最近更新 更多