Advertisement
Guest User

Untitled

a guest
Sep 29th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. nohaelprince@uwaterloo.ca, 01-05-2014
  2. nohaelprince@uwaterloo.ca, 01-05-2014
  3. nohaelprince@uwaterloo.ca, 01-05-2014
  4. nohaelprince@gmail.com, 01-05-2014
  5.  
  6. INSERT INTO domains(domain_name, cnt, date_of_entry) VALUES (%s, %s, %s);
  7.  
  8. #!/usr/bin/python
  9. import fileinput
  10. import csv
  11. import os
  12. import sys
  13. import MySQLdb
  14.  
  15. from collections import defaultdict
  16.  
  17. lst = defaultdict(list)
  18. d_lst = defaultdict(list)
  19.  
  20. # ======================== Defined Functions ======================
  21. def get_file_path(filename):
  22. currentdirpath = os.getcwd()
  23. # get current working directory path
  24. filepath = os.path.join(currentdirpath, filename)
  25. return filepath
  26. # ===========================================================
  27. def read_CSV(filepath):
  28.  
  29. domain_list = []
  30. domain_date_list = []
  31. sorted_domain_list_bydate = defaultdict(list)
  32.  
  33. with open(filepath, 'rb') as csvfile:
  34. reader = csv.reader(csvfile)
  35.  
  36. for row in reader:
  37. # insert the 1st & 2nd column of the CSV file into a set called input_list
  38. email = row[0].strip().lower()
  39. date = row[1].strip()
  40.  
  41. domain_date_list.append([date, email[ email.find("@") : ]])
  42. domain_list.append(email[ email.find("@") : ])
  43.  
  44. for k, v in domain_date_list:
  45. sorted_domain_list_bydate[k].append(v)
  46.  
  47.  
  48. # remove duplicates from domain list
  49. domain_list = list(set(domain_list))
  50.  
  51. return sorted_domain_list_bydate, domain_list
  52. # ===========================================================
  53. def update_DB(lst):
  54.  
  55. # open a database connection
  56. db = MySQLdb.connect(host="localhost", # your host, usually localhost
  57. user="root", # your username
  58. passwd="abcdef1234", # your password
  59. db="test") # name of the data base
  60. cur = db.cursor()
  61.  
  62. a = []
  63. for k, v in lst.items():
  64. # now what should I do here?
  65. # this is what I am confuse
  66.  
  67. db.commit()
  68. db.close()
  69. # ==========================================================
  70.  
  71. # ======================= main program =======================================
  72. path = get_file_path('emails.csv')
  73. [lst, d_lst] = read_CSV(path) # read the input file
  74. update_DB(lst) # insert data into domains table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement