Advertisement
Guest User

Untitled

a guest
Apr 1st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import csv
  2.  
  3. import psycopg2
  4. import psycopg2.extras
  5.  
  6. import sys
  7.  
  8. conn_string = "host='localhost' dbname='postgres' user='postgres' password='Hockeyboy10'"
  9.  
  10.  
  11. def get_cursor():
  12. conn = psycopg2.connect(conn_string)
  13. cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  14. return cursor
  15.  
  16.  
  17. def run_query(cursor, query):
  18. cursor.execute(query)
  19. records = cursor.fetchall()
  20. return records
  21.  
  22.  
  23. def load_query(path):
  24. with open(path, 'r') as query_file:
  25. contents = query_file.readlines()
  26. lines = map(lambda bytes: str(bytes), contents)
  27. no_comments = filter(lambda line: not line.lstrip().startswith('--'), lines)
  28. no_bom = list(map(lambda line: line.replace('\xef\xbb\xbf', ''), no_comments))
  29. joined = ''.join(no_bom)
  30. split = joined.split(';')
  31. return split[0]
  32.  
  33. def createTeamTable(conn, cursor):
  34. try:
  35. cursor.execute("DROP TABLE IF EXISTS teams")
  36. cursor.execute("CREATE TABLE teams( \
  37. teamid VARCHAR(3), \
  38. team_name VARCHAR(63) \
  39. )")
  40. conn.commit()
  41. except Exception as e:
  42. print(e)
  43. conn.rollback()
  44.  
  45. def createAmateurTable(conn, cursor):
  46. try:
  47. cursor.execute("CREATE TABLE amateur_teams( \
  48. team_name VARCHAR(63), \
  49. league VARCHAR(63) \
  50. )")
  51. conn.commit()
  52. except Exception as e:
  53. print(e)
  54. conn.rollback()
  55.  
  56.  
  57. def main():
  58.  
  59. createTeamTable(conn, cursor)
  60. teams = set()
  61.  
  62. with open('teams.csv', mode='r') as csv_file:
  63. csv_reader = csv.reader(csv_file, delimiter=',', quotechar='|')
  64. for row in csv_reader:
  65. teams.add((row[1], row[0]))
  66.  
  67. for abbr, name in teams:
  68. cursor.execute("insert into teams (teamid, team_name) \
  69. values('{}', '{}')".format(abbr, name))
  70.  
  71. am_teams = set()
  72. with open('hockey_ref.csv', mode='r') as csv_file:
  73. csv_reader = csv.reader(csv_file, delimiter=',', quotechar='|')
  74. for row in csv_reader:
  75. curString = row[8].split()
  76. if curString[len(curString)-1][0] == '(':
  77. league = curString[len(curString)-1].lower()
  78. teamName = curString[0].lower()
  79. for i in range(1, len(curString)-1):
  80. teamName = teamName + " " + curString[i].lower()
  81. print teamName.count("'")
  82. teamName.replace("'", "''")
  83. am_teams.add((teamName, league))
  84.  
  85. conn = psycopg2.connect(conn_string)
  86. cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  87. #createAmateurTable(conn, cursor)
  88.  
  89. for team, league in am_teams:
  90. print team, league
  91. cursor.execute("insert into amateur_teams (team_name, league) \
  92. values('{}', '{}')".format(team, league))
  93.  
  94.  
  95. if __name__ == '__main__':
  96. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement