Skip to content

Instantly share code, notes, and snippets.

@rohitdholakia
Created December 25, 2011 09:05
Show Gist options
  • Save rohitdholakia/1518970 to your computer and use it in GitHub Desktop.
Save rohitdholakia/1518970 to your computer and use it in GitHub Desktop.
Script to find similar data for users
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 userId="+str(id))
return cursor.fetchone()
def getRatings(cursor,id):
cursor.execute("select count(*) from allRatings where userId="+str(id))
return cursor.fetchone()
cursor = connect()
cursor.execute("select distinct(userId) from allRatings")
listUsers=[]
for i in cursor.fetchall():
listUsers.append(i[0])
forMovies = open(sys.argv[1],'w')
for user in listUsers:
avg = getAverage(cursor,user)[0]
number = getRatings(cursor,user)[0]
forMovies.write(str(user)+","+str(avg)+","+str(number)+"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment