Created
April 9, 2014 11:16
-
-
Save otknoy/10256402 to your computer and use it in GitHub Desktop.
Python で正規表現を用いて日付表現を抽出する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
def extract_date(s): | |
date_pattern = re.compile('(\d{4})/(\d{1,2})/(\d{1,2})') | |
result = date_pattern.search(s) | |
if result: | |
y, m, d = result.groups() | |
return {'year': y, 'month': m, 'day': d} | |
else: | |
return None | |
if __name__ == '__main__': | |
import sys | |
filename = sys.argv[1] | |
f = open(filename) | |
text = f.read() | |
f.close() | |
for line in text.splitlines(): | |
result = extract_date(line) | |
if result: | |
print result | |
print line | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment