Skip to content

Instantly share code, notes, and snippets.

@hancush
Last active January 4, 2019 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hancush/f8ad9b07678bcf6c58186c716f3e6cb3 to your computer and use it in GitHub Desktop.
Save hancush/f8ad9b07678bcf6c58186c716f3e6cb3 to your computer and use it in GitHub Desktop.
# 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