Created
December 24, 2011 21:02
-
-
Save rohitdholakia/1518336 to your computer and use it in GitHub Desktop.
A python script to find average ratings for all movies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="+id) | |
return cursor.fetchone() | |
def getRatings(cursor,id): | |
cursor.execute("select count(*) from allRatings where movieId="+id) | |
cursor = connect() | |
forMovies = open(sys.argv[1],'w') | |
for i in range(1,17771): | |
avg = getAverage(cursor,i) | |
number = getRatings(cursor,i) | |
forMovies.write(i+","+avg+","+number+"\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment