I have a QR reader. When the QR reader scans, I want to monitor what it scans, But i've run into a weird problem, and as i'm so new to Python I really have no idea why its happening. Okay so, Below are two seemingly (to me) identical programs, apart from line. When that line is removed, I get different results (seemingly the charset changes). I REALLY dont understand why.
test2.py:
# -*- coding: utf-8 -*-
import pyHook
import pythoncom
import re
import webbrowser
chars = ""
def pressed_chars(event):
global chars
if event.Ascii:
char = chr(event.Ascii)
if event.Ascii == 3:
quit()
else:
chars += char
print chars
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
Produces the following output when scanning a QR code with the contents http://google.com
:
H
HT
HTT
HTTP
HTTP:
HTTP:?
HTTP:??
HTTP:??G
HTTP:??GO
HTTP:??GOO
HTTP:??GOOG
HTTP:??GOOGL
HTTP:??GOOGLE
HTTP:??GOOGLE>
HTTP:??GOOGLE>C
HTTP:??GOOGLE>CO
HTTP:??GOOGLE>COM
HTTP:??GOOGLE>COM
And now test3.py:
# -*- coding: utf-8 -*-
import pyHook
import pythoncom
import re
import webbrowser
endDomains = ".com|.net|.org|.edu|.gov|.mil|.aero|.asia|.biz|.cat|.coop|.info|.int|.jobs|.mobi|.museum|.name|.post|.pro|.tel|.travel".split("|")
chars = ""
def pressed_chars(event):
global chars
if event.Ascii:
char = chr(event.Ascii)
if event.Ascii == 3:
quit()
else:
chars += char
print chars
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
Produces the following output:
h
ht
htt
http
http;
http;/
http;//
http;//g
http;//go
http;//goo
http;//goog
http;//googl
http;//google
http;//google.
http;//google.c
http;//google.co
http;//google.com
http;//google.com
If I remove any part of the endDomains
variable, the program changes. Is there some characters that I cant see that I'm removing or something that changes things? Why on earth is python producing these two completely different results when removing a variable that the program doesn't even use?
Edit: It seems to be connected to the .split("|")
, and rather not the variable. If I remove the .split("|")
the program breaks again.
Second Edit Credit for the original source that I used goes to Janekmuric from their answer here.