使用Python在文件中查找匹配的字符串

时间:2016-06-09 13:35:41

标签: python regex python-3.x

使用Python我想在符合此格式的文件中找到字符串YYYY-MM-DD

Here is how my sample file looks like
I want to find date 2016-01-01 ,2016-01-05
then I want to find 2016-01-17
then I want to find this date 2016-01-04

输出应为

2016-01-01
2016-01-05
2016-01-17
2016-01-04

下面是我目前正在使用的代码,但我无法找到匹配的记录,对此有任何帮助将不胜感激?

#!/usr/bin/python
import sys
import csv 
import re
pattern = re.compile("^([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])$")
for i, line in enumerate(open('C:\\Work\\scripts\\logs\\CSI.txt')):
    for match in re.finditer(pattern, line):
        print 'Found on line' % (i+1, match.groups())

2 个答案:

答案 0 :(得分:0)

我会删除^(和$,因为你的日期似乎没有分开:

re.compile("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]")

答案 1 :(得分:0)

您可以使用正则表达式和日期时间从字符串

获取有效日期
import re
from datetime import datetime
string = "I want to find date 2016-01-01 ,2016-01-05"
pattern  = re.complie("[\d]{4}-\d{2}-\d{2}")
raw_dates = pattern.findall(string)
parsed_dates = []
for date in raw_dates:
   try:
      d = datetime.strptime(date, "%Y-%m-%d")
      parsed_dates.append(d)
   except:
      pass
print(parsed_dates)

输出:

['2016-01-01', '2016-01-05']