Skip to content

Instantly share code, notes, and snippets.

@rohitdholakia
Created December 25, 2011 09:04
Show Gist options
  • Save rohitdholakia/1518969 to your computer and use it in GitHub Desktop.
Save rohitdholakia/1518969 to your computer and use it in GitHub Desktop.
Python script to find movie data
import sys,os
import MySQLdb
def connect():
conn = MySQLdb.connect (host = "localhost",user = "root",db = "netflix")
cursor=conn.cursor()
return cursor
def getAverage(cursor,id):
#Take a movieId and find out the average rating for that movie
cursor.execute("select avg(rating) from allRatings where movieId="+str(id))
return cursor.fetchone()
def getRatings(cursor,id):
cursor.execute("select count(*) from allRatings where movieId="+str(id))
return cursor.fetchone()
cursor = connect()
forMovies = open(sys.argv[1],'w')
for i in range(1,17771):
avg = getAverage(cursor,i)[0]
number = getRatings(cursor,i)[0]
forMovies.write(str(i)+","+str(avg)+","+str(number)+"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment