Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def unescape(s):
- s = s.replace("<", "<")
- s = s.replace(">", ">")
- # this has to be last:
- s = s.replace("&", "&")
- return s
- # {bookId: {userId: rating,...
- dic = {}
- fin = open('/root/Downloads/bx/BX-Dump/BX-Book-Ratings.csv')
- for line in fin:
- book1 = unescape(line.strip())
- book = book1.split('";"')
- if book[1].strip('"') not in dic:
- dic[book[1].strip('"')] = {}
- dic[book[1].strip('"')][book[0].strip('"')] = book[2].strip('"')
- else:
- dic[book[1].strip('"')][book[0].strip('"')] = book[2].strip('"')
- # {Number of reviews: bookId
- dic1 = {}
- for a in dic:
- b = len(dic[str(a)])
- try:
- if b not in dic1:
- dic1[b] = [a]
- else: dic1[b].append(a)
- except:
- pass
- # [number of reviews,...
- popularbooks = dic1.keys()
- popularbooks.sort()
- popularbooks.reverse()
- # {bookId: title
- dic2 = {}
- fin = open('/root/Downloads/bx/BX-Dump/BX-Books.csv')
- for line in fin:
- book1 = unescape(line.strip())
- book = book1.split('";"')
- dic2[book[0].strip('"')] = book[1].strip('"')
- # {userId: age,...
- useragedic = {}
- fin = open('/root/Downloads/bx/BX-Dump/BX-Users.csv')
- for line in fin:
- frogs = line.replace('"','').strip().split(";")
- if frogs[0] not in useragedic:
- try:
- useragedic[frogs[0]] = int(frogs[2])
- except:
- useragedic[frogs[0]] = int(0)
- count = 0
- total = 0
- print "\n---------------------------------------------\n"
- for a in popularbooks:
- for b in dic1[a]:
- if count == 70:
- continue
- else:
- try:
- print a,"Reviews\nBookTitle:", dic2[str(b)],"\nBookId:", str(b)
- count += 1
- count1 = 0
- total = 0
- for c in dic[str(b)]:
- # skip if user age is unspecified
- if useragedic[c] == 0:
- pass
- else:
- total += int(useragedic[c])
- count1 += 1
- avageofrev = float(total/count1)
- print "Average age of reviewer =",avageofrev,"\n\n"
- except:
- print "error with value",str(b),"\n"
- """ find the ten most popular books to review
- reviews title
- 2502 Wild Animus
- 1295 The Lovely Bones: A Novel
- 883 The Da Vinci Code
- 732 Divine Secrets of the Ya-Ya Sisterhood: A Novel
- 723 The Red Tent (Bestselling Backlist)
- 647 A Painted House
- 639 error with value 0679781587
- 615 The Secret Life of Bees
- 614 Snow Falling on Cedars
- 586 Angels & Demons
- 585 Where the Heart Is (Oprah's Book Club (Paperback))
- concerning '639 error with value 0679781587'
- >>>for a in dic2:
- >>> if '79587' in a: print a
- 0674795873
- 0915795876
- 0840795874
- 0671795872
- conclusion :
- 0679781587 is a value in BX-Book-Ratings.csv
- that doesnt appear in BX-Books.csv
- """
Advertisement
Add Comment
Please, Sign In to add comment