是否可能有一个“catch all”语句,该语句会影响for match in re.findall
语句尚未添加的所有内容。
for submission in submissions:
print (submission.url)
for match in re.findall(".+imgur\.com+",submission.url):
NonFilteredImgurImageList.append(submission.url)
for match in re.findall("gfycat\.com",submission.url):
GfycatUrlList.append(submission.url)
else:
NonStandardUrlList.append(submission.url)
我试过上面,但那没用。
有没有办法让它工作或更好的设置方式(我计划随着时间的推移添加更多的正则表达式搜索)。
答案 0 :(得分:0)
看起来你不需要正则表达式。实际上,如果这些submission.url
调用实际上包含多个结果,则会附加相应in
个字符串的重复项。只需使用for submission in submissions:
print(submission.url)
if 'imgur.com' in submission.url:
NonFilteredImgurImageList.append(submission.url)
elif 'gifycat.com' in submission.url:
GfycatUrlList.append(submission.url)
else:
NonStandardUrlList.append(submission.url)
。
for submission in submissions:
i = re.findall(expression1, submissions.url)
g = re.findall(expression2, submissions.url)
if i:
results['imgur'] += i
elif g:
results['gfycat'] += g
else:
results['failures'].append(submissions.url)
如果此处实际需要正则表达式匹配,您将使用以下内容:
import getch
import threading
light_on = True
def countdown(count_from):
global light_on
print("Light will turn off in...")
x = count_from
while not light_on:
print x
time.sleep(1)
x -= 1
quit(0)
def testkey(keyname):
global light_on
while 1:
char = getch.getch()
if char == keyname:
light_on = True
threading.Thread(target=testkey, args=('\n')).start()
while 1:
if light_on: print("ON!")
light_on = False
threading.Thread(target=countdown, args=(10)).start()