[英]Strange behavior in Python, Line missing, different outputs
I have a QR reader. 我有一个QR阅读器。 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.
当QR阅读器扫描时,我想监视它扫描的内容,但我遇到了一个奇怪的问题,因为我是Python的新手,我真的不知道它为什么会发生。 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).
当删除该行时,我会得到不同的结果(看似charset更改)。 I REALLY dont understand why.
我真的不明白为什么。
test2.py: 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
: 使用内容
http://google.com
扫描QR码时产生以下输出:
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: 现在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. 如果我删除了
endDomains
变量的任何部分,程序就会更改。 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?
为什么在删除程序甚至不使用的变量时,python会产生这两个完全不同的结果?
Edit: It seems to be connected to the .split("|")
, and rather not the variable. 编辑:它似乎连接到
.split("|")
,而不是变量。 If I remove the .split("|")
the program breaks again. 如果删除
.split("|")
,程序会再次中断。
Second Edit Credit for the original source that I used goes to Janekmuric from their answer here. 我使用的原始来源的第二个编辑信用来自他们的回答Janenamuric。
The way you have defined endDomains is not very 'pythonic' 你定义endDomains的方式不是很'pythonic'
it would be better to just define it as a list. 将它定义为列表会更好。
endDomains = [
".com", ".net", ".org", ".edu", ".gov", ".mil", ".aero", ".asia",
".biz", ".cat", ".coop", ".info", ".int", ".jobs", ".mobi", ".museum",
".name", ".post", ".pro", ".tel", ".travel"
]
Are you able to return the value and have it picked up so the construction happens outside the event? 您是否能够返回该值并将其拾取,以便在事件之外进行构建? Unfortunately I'm not on windows so I can not test this.
不幸的是我不在Windows上,所以我无法测试这个。 I did write this:
我写了这个:
"""Mock."""
endDomains = [
".com", ".net", ".org", ".edu", ".gov", ".mil", ".aero", ".asia",
".biz", ".cat", ".coop", ".info", ".int", ".jobs", ".mobi", ".museum",
".name", ".post", ".pro", ".tel", ".travel"
]
chars = ""
def pressed_chars(event):
global chars
char = chr(ord(event))
if event == '3':
return False
else:
chars += char
print(chars)
return True
result = True
while result:
string = input("Some input please: ")
for character in string:
result = pressed_chars(character)
print("Done")
This does not suffer from the same issue. 这不会遇到同样的问题。 So I would expect that the issue in pyHook or pythoncom.
所以我希望pyHook或pythoncom中的问题。 Also it's worth noting globals are not typically used and because it adds to confusion: Use of "global" keyword in Python
另外值得注意的是通常不使用全局变量,因为它增加了混乱: 在Python中使用“global”关键字
hope that helps. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.