Last active
January 4, 2019 01:43
-
-
Save hancush/f8ad9b07678bcf6c58186c716f3e6cb3 to your computer and use it in GitHub Desktop.
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
# usage: python interval.py /path/to/your/file.csv | |
import csv | |
import sys | |
import re | |
def days_from_interval(interval): | |
if interval == 'never': | |
return None | |
days_ago = 0 | |
years = (r'\d{1,}(?= years?)', 365) | |
months = (r'\d{1,}(?= months?)', 30) | |
weeks = (r'\d{1,}(?= weeks?)', 7) | |
days = (r'\d{1,}(?= days?)', 1) | |
for pattern, multiplier in (years, months, weeks, days): | |
match = re.search(pattern, interval) | |
if match: | |
product = int(match.group(0)) * multiplier | |
else: | |
product = 0 | |
days_ago += product | |
return days_ago | |
if __name__ == '__main__': | |
_, infile = sys.argv | |
with open(infile, 'r') as f: | |
reader = csv.reader(f) | |
writer = csv.writer(sys.stdout) | |
writer.writerow(next(reader)) | |
for row in reader: | |
row[2] = days_from_interval(row[2]) | |
row[3] = days_from_interval(row[3]) | |
writer.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment