Created
March 22, 2016 15:58
-
-
Save otech-nl/702f91bc1c216f8fc660 to your computer and use it in GitHub Desktop.
This script takes the Radio 2 Top 2000 and analyses artist performance
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
''' This script takes the Radio 2 Top 2000 (http://www.nporadio2.nl/top2000) | |
and analyses artist performance: | |
- count: number of occurences of artist in list | |
- score: number of points (#1 song gets 2000 point, #2 song gets 1999 etc.) | |
store input file as CSV with ;-delimiter in 'TOP-2000-2015.csv' | |
''' | |
import csv | |
import sys | |
def main(csvfile, score): | |
print >>sys.stderr, "Reading %d lines from %s" % (score, csvfile) | |
# read and analyze | |
with open('TOP-2000-2015.csv') as csvfile: | |
artists = dict() | |
reader = csv.DictReader(csvfile, delimiter=';') | |
score = 2000 | |
for row in reader: | |
artist = row['artiest'] | |
if artist in artists: | |
artist = artists[artist] | |
artist['count'] += 1 | |
artist['score'] += score | |
else: | |
artists[artist] = dict(name=artist) | |
artist = artists[artist] | |
artist['count'] = 1 | |
artist['score'] = score | |
score -= 1 | |
# convert to list | |
artist_list = [] | |
for name, data in artists.iteritems(): | |
artist_list.append(data) | |
# write as sorted CSV to stdout | |
writer = csv.DictWriter(sys.stdout, fieldnames='name count score'.split(), lineterminator='\n') | |
writer.writeheader() | |
for artist in sorted(artist_list, reverse=True, key=lambda artist: artist['score']): | |
writer.writerow(artist) | |
if __name__ == "__main__": | |
sys.exit(main('TOP-2000-2015.csv', 2000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment