2012-08-14 34 views
1

我有一個記錄器,用於監聽諸如方程式之類的序列,但我需要刪除像「L_control」或「shift」之類的所有按鍵,我所做的是獲取ascii數字,然後使用chr(event.Ascii),但是它隨着ctrl和shift鍵作爲空間返回。是數字,aplha還是數學符號

我目前正在使用它來刪除我不想要的所有字符,但它似乎不起作用。任何改進的想法?

def removeChars(l): 
    acceptedChars = ["[", "]", "+", "-", "/", "*", "^", "*", "(", ")"] 
    newL = "" 
    for x in range(0, len(l)): 
     if l[x].isalpha() or l[x] in acceptedChars or l[x].isdigit(): 
      newL = newL + l[x] 
    return newL 

編輯:

我使用pyHook拿到鑰匙事件,並使用event.Ascii獲得ASCII值,然後使用chr(event.Ascii)得到字符

+0

你是如何得到這個' l'對象?你不應該得到原始按鍵。 – Falmarri 2012-08-14 21:57:20

+1

輸出是什麼?而'l'沒有定義。那麼輸入是什麼?請提供樣本輸入和輸出。 – User007 2012-08-14 21:57:49

+0

l是方法/函數中的一個參數,返回l,我修正它,l是一個字符串,Im使用pyHook和pythoncom來完成,當有一個按鍵'onKeyEvent(event)'被調用時,那麼你可以調用'event.Ascii'來獲取ascii值,然後你可以使用'chr(event.Ascii)'來獲取字符 – FabianCook 2012-08-14 22:00:15

回答

2

什麼這樣的事情?

import string 
acceptedChars = set(string.printable) 
newL = ''.join([ x for x in l if x in acceptedChars]) 

編輯:

您可以使用set()任何以匹配例如只得到數字,字母,並選擇符號:

acceptedChars = set(string.digits + "[]()+-/*^=!<>" + string.letters) 
newL = ''.join([ x for x in l if x in acceptedChars]) 
+0

數字和字母字符怎麼樣?我想刪除像space,fullstops等東西 – FabianCook 2012-08-14 22:04:59

+0

你可以使用任何設置爲acceptedChars,如set(string.printable) - set(string.whitespace)或set(string.ascii_letters)+ set(string.digits)+ set (string.punctuation) – JeffS 2012-08-14 22:10:10

+0

甜,作爲,謝謝 – FabianCook 2012-08-14 22:16:56

相關問題