解析存储日期和时间的通用字符串 [英] Parsing of generic string that stores day and time
问题描述
我需要生成一个函数,根据以下数据查找可以开会的员工。如果我通过有效时间,它应该返回在那个小时内有空的人。
1234 Mon-Tue 09:30 am - 下午1:00
2345周一至周四下午1:00 - 下午3:00,周五上午10:30 - 下午3:00
456周一至周三上午9:00 - 上午9:30 ,星期二上午10:00 - 下午3:00,星期五上午10:00 - 下午2:00
我需要拆分第二个值,即空闲时间并检查用户是否有空。
我的挑战在于如何将半结构化数据拆分成一个列表并搜索我想要的值。
谢谢,
我尝试过:
这就是我的尝试。
#myfile.csv中的我的数据
user1周一至周二上午11:30 - 下午1:00
user2 Mon-下午2:00 - 下午3:00,周五上午11:30 - 下午4:00
user3周一至周三上午9:00 - 上午9:30,周二上午10:00 - 3 :00:00 pm,周五上午10:00 - 下午2:00
user4周一上午10:00 - 上午10:30,周二下午1:00 - 下午3:00,周五上午9:00 - 上午10:00
user5周五上午11:00 - 上午11:30
user6周一至周五9:00 am - 4:30 pm
user7周一至周三上午10:30 - 上午11:30,周二上午11:30 - 下午3:00,周五上午10:00 - 下午2:00
user8星期一上午10:30 - 上午11:30,星期二上午10:00 - 下午3:00,星期五上午11:00 - 下午2:00
user9星期一-Wed上午9:00 - 上午9:30,周二上午10:00 - 下午3:00,周五上午10:00 - 下午2:00
user10星期二上午10:00 - 上午11:30,周五上午10:00 - 下午2:00
user11周三上午11:00 - 下午3:30,周五上午11:00 - 下午2:00
user12星期一上午9:00 - 下午3:30,星期五上午11:00 - 下午2:00
user13星期五上午11:00 - 下午3:30
user14 Wed 12:00 am - 12:30 pm
user16星期五上午10:00 - 中午12:00
import csv
import re
userid = []
dates = [ ]
num = []
let = []
pattern = re.compile(r'\([AZ] [az] {2})\ s ?*(( - )\s *([AZ] [AZ] {2}))\s *(\d \d:\\? \\ n \ * [aApP] [Mm])')
with open('myfile.csv')as csvDataFile:
csvReader = csv.reader(csvDataFile)
表示csvReader中的行:
userid.append(row [0])
list_val = [item.strip()表示行[1]中的项目.split(',')]
dates.append(list_val)
打印日期,list_val
a =过滤器(pattern.match,日期)
print str(a)过滤器中的(字母,数字)
(模式.match,dates):
num.append(数字)
let.append(字母)
打印(数字,'*',字母)
请参阅 8.1。 datetime - 基本日期和时间类型 - Python 2.7.14文档 [ ^ ]。
I need to generate a function that finds the employee who is free for a meeting based on the following data. If I pass a valid time, it should return back who is free during that hour.
1234 Mon-Tue 09:30 am - 1:00 pm
2345 Mon-Thu 1:00 pm - 3:00 pm , Fri 10:30 am - 3:00 pm
456 Mon-Wed 9:00 am - 9:30 am , Tue 10:00 am - 3:00 pm , Fri 10:00 am - 2:00 pm
I need to split the second value, i.e free time and check if the user is free.
My challenge is around how do I split semi structured data into a list and search my desired value.
Thanks,
What I have tried:
This is what I tried.
# My data in myfile.csv
"""
user1 Mon-Tue 11:30 am - 1:00 pm
user2 Mon-Thu 2:00 pm - 3:00 pm , Fri 11:30 am - 4:00 pm
user3 Mon-Wed 9:00 am - 9:30 am , Tue 10:00 am - 3:00 pm , Fri 10:00 am - 2:00 pm
user4 Mon 10:00 am - 10:30 am , Tue 1:00 pm - 3:00 pm , Fri 9:00 am - 10:00 am
user5 Fri 11:00 am - 11:30 am
user6 Mon-Fri 9:00 am - 4:30 pm
user7 Mon-Wed 10:30 am - 11:30 am , Tue 11:30 am - 3:00 pm , Fri 10:00 am - 2:00 pm
user8 Mon 10:30 am - 11:30 am , Tue 10:00 am - 3:00 pm , Fri 11:00 am - 2:00 pm
user9 Mon-Wed 9:00 am - 9:30 am , Tue 10:00 am - 3:00 pm , Fri 10:00 am - 2:00 pm
user10 Tue-Wed 10:00 am - 11:30 am , Fri 10:00 am - 2:00 pm
user11 Wed 11:00 am - 3:30 pm , Fri 11:00 am - 2:00 pm
user12 Mon 9:00 am - 3:30 pm , Fri 11:00 am - 2:00 pm
user13 Fri 11:00 am - 3:30 pm
user14 Wed 12:00 am - 12:30 pm
user16 Fri 10:00 am - 12:00 pm
"""
import csv import re userid = [] dates = [] num = [] let = [] pattern = re.compile(r'\b([A-Z][a-z]{2})\s*((-)\s*([A-Z][a-z]{2}))?\s*(\d?\d:\d\d *[aApP][Mm])') with open('myfile.csv') as csvDataFile: csvReader = csv.reader(csvDataFile) for row in csvReader: userid.append(row[0]) list_val = [item.strip() for item in row[1].split(',')] dates.append(list_val) print dates, list_val a = filter(pattern.match,dates) print str(a) for (letters, numbers) in filter(pattern.match,dates): num.append(numbers) let.append(letters) print(numbers, '*', letters)
See 8.1. datetime — Basic date and time types — Python 2.7.14 documentation[^].
这篇关于解析存储日期和时间的通用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!