Advertisement
Guest User

CSV

a guest
Dec 13th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. from datetime import datetime
  2.  
  3. dates = {}
  4. with open('v.csv') as f:  # Path to csv file
  5.     for line in f.readlines():
  6.         values = line.strip().split(',')  # List of comma-separated values, strip() used to get rid of '\n' symbol in the end
  7.         user_id = values[3]  # Index of id
  8.         date = datetime.strptime(values[6], '"%Y-%m-%d %H:%M:%S"')  # Parsing sate and time from a string to datetime object
  9.         if user_id in dates:  # If we already have a date for this user
  10.             dates[user_id] = min(date, dates[user_id])  # Minimum of date we just read and the one we recorded before
  11.         else:
  12.             dates[user_id] = date  # Date we just read
  13.  
  14. for item in dates.items():
  15.     print("User with id {} made first purchase on {}".format(item[0], item[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement