编辑:看来我的错误是由我的一个简单疏忽引起的,应该是python中的。谢谢大家的帮助!
我正在尝试自学python,因此我想编写一个简单的脚本来计算我当前工作的工作时间。似乎我找不到在条件if和elif语句中如何使用正则表达式的任何好资源。
我要做的是检查开始时间是否为上午或下午,结束时间是否为上午或下午,具体取决于组合,我可以准确地计算出总工作时间
为此,我使用正则表达式来检查输入的上午或下午。然后,我查看发送的时间是哪种组合, 例如start_time = 8:00 am和end_time = 11:23 am,因此应该具有start_match_am和end_match_am的匹配项
我的完整代码如下:
import re
pattern = re.compile('^([0-1]?[0-9]):[0-5][0-9][aApPmM]{2}$')
start_time = ""
end_time = ""
hours_worked = 0.0
start_time = input("Please enter the time you started working(ex -- 8:00am): \n")
while re.match(pattern, start_time) == False:
print("I'm sorry the time must be in the format -- HH:MMam or H:MMpm \n")
start_time = input("Please enter the time you started working(ex -- 8:00am): \n")
end_time = input("Please enter the time you stopped working(ex -- 5:00pm): \n")
while re.match(pattern, end_time) == False:
print("I'm sorry the time must be in the format -- HH:MMam or H:MMpm \n")
end_time = input("Please enter the time you stopped working(ex -- 8:00am): \n")
start_hour = int(start_time[:(start_time.find(':'))])
start_minutes = int(start_time[(start_time.find(':') + 1):-2])
end_hour = int(end_time[:(end_time.find(':'))])
end_minutes = int(end_time[(end_time.find(':') + 1):-2])
start_period = start_time[start_time.find(':')+3:]
end_period = end_time[end_time.find(':')+3:]
pattern_am = re.compile('am', re.I)
pattern_pm = re.compile('pm', re.I)
start_match_am = re.match(pattern_am, start_period)
start_match_pm = re.match(pattern_pm, start_period)
end_match_am = re.match(pattern_am, end_period)
end_match_pm = re.match(pattern_pm, end_period)
if start_match_am is not None & end_match_am is not None:
total_hours = end_hour - start_hour
total_minutes = (end_minutes - start_minutes+60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
elif start_match_pm is not None & end_match_pm is not None:
total_hours = end_hour - start_hour
total_minutes = (end_minutes - start_minutes+60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
elif start_match_am is not None & end_match_pm is not None:
total_hours = end_hour + 12 - start_hour
total_minutes = (end_minutes - start_minutes + 60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
elif start_match_pm is not None & end_match_am is not None:
total_hours = end_hour + 12 - start_hour
total_minutes = (end_minutes - start_minutes + 60) % 60
print("You worked for "+total_hours+"hr "+total_minutes+"min")
首先,我想说的是,我知道我现在无法正确地增加分钟数,我写道,当我很累并且要使正则表达式工作时,它会修复。该问题具体发生在以下行:
if start_match_am is not None & end_match_am is not None:
我得到的错误是:
Please enter the time you started working(ex -- 8:00am):
8:00am
Please enter the time you stopped working(ex -- 5:00pm):
11:23am
Traceback (most recent call last):
File "/home/primus/PycharmProjects/HoursWorks/venv/HoursWorked/CalculateHours.py", line 27, in <module>
if start_match_am is not None & end_match_am is not None:
TypeError: unsupported operand type(s) for &: 'NoneType' and '_sre.SRE_Match'
Process finished with exit code 1
这真的让我感到难过,因为这里的以下帖子特别指出这是您在条件if语句中使用正则表达式的方式:
Python: How to use RegEx in an if statement?
Regular Expression in conditional statement using Python 3
如果有人可以指出我犯的一个简单错误,或者让处于类似情况的其他人知道如何在python 3中的条件if语句中正确使用正则表达式,将不胜感激!
答案 0 :(得分:1)
re.search返回匹配对象或无。首先,调用re.search并将其值存储在变量中,然后测试其是否为None。尝试从中提取值之前,只需确保测试是否为“无”即可。
此外,您无需使用is not None
来检查某项是否等于零,只需将其视为布尔值即可对其进行测试。
最后,您似乎想从正则表达式中提取数据。如果要执行此操作,则可以在正则表达式上调用group(0)
以获取匹配的内容(实际上是第一组,但是您可以在正则表达式页面上阅读有关此内容的更多信息,这应该可以满足您的目的)。然后将结果转换为所需的任何内容。例如,您可以找到并提取字符串中的第一个数字,然后测试它是否大于某个值,如下所示:
import re
my_str = input()
match = re.search(r'\d+', my_str)
if match and int(match.group(0)) > 10:
print('The input was greater than 10.')
希望这会有所帮助。如果您想了解有关正则表达式的更多信息,请查看文档。 文件:Binding
答案 1 :(得分:0)
请注意,我知道我的答案不是您所提问题的答案-但是,我觉得告诉您以下内容仍然很重要。
Regex是一个非常强大的工具,具有目标应用程序或要解决的问题。但是IMO,您的不是正则表达式主题。
有一个用于python的库,可以处理日期和时间。请考虑使用这种方法:
import datetime as DT
start_time = input("Please enter the time you started working(ex -- 8:00am): \n")
incorr_ans = True
while incorr_ans:
try:
s_t = DT.datetime.strptime(start_time, '%I:%M%p')
incorr_ans = False
except:
start_time = input("Please enter the time you started working(ex -- 8:00am): \n")
end_time = input("Please enter the time you stopped working(ex -- 8:00am): \n")
incorr_ans = True
while incorr_ans:
try:
e_t = DT.datetime.strptime(end_time, '%I:%M%p')
incorr_ans = False
except:
end_time = input("Please enter the time you stopped working(ex -- 8:00am): \n")
print(s_t, e_t)
print(e_t - s_t)
请注意,该程序的主要部分是由于捕获用户输入的错误加倍。但是我仍然认为这是一种非常方便的方法。并且:您的复杂计算只是这里的简单减法...
因此,这将从用户角度生成程序,例如:
Please enter the time you started working(ex -- 8:00am):
8:34am
Please enter the time you stopped working(ex -- 8:00am):
3:07pm
1900-01-01 08:34:00 1900-01-01 15:07:00
6:33:00
只需仔细阅读它,也许您会喜欢它并自己朝这个方向努力。
而且,如果您真的真的测试re
,因为它很有趣-还会有其他问题……