Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Python program to display formatted answers.json totals?

A question popped up a few days ago about how to view data from the JSON files in Y!A user data downloads.  Here's a simple Python program to summarize the contents of the "answers.json" file, if anybody wants it.

I was somewhat surprised that my totals were a bit *less* that what's on the US Programming & Design leaderboard.  Very close, even though those numbers haven't changed in at least a year.  Back-end code for this board was worse than I guessed!

#

import os,sys

import json

import collections

ya_ansfile = open('answers.json', 'rb')

ya_ansdata = ya_ansfile.read()

ya_ans = json.loads(ya_ansdata)

# count all answers flagged as "best" ("favorite")

print(sum((a["userAnswer"].get("isBestAnswer")) for a in ya_ans))

# Get counts of answers and best answers by category:

allcount = collections.Counter(a['category']['name'] for a in ya_ans)

besties = collections.Counter(a['category']['name'] for a in ya_ans

                              if a['userAnswer']['isBestAnswer'])

# Print category totals sorted by category name:

print()

print("  total   best  category")

for cat in sorted(allcount.keys()):

    print(" {:6d} {:5d}   {}".format(allcount[cat], besties.get(cat,0), cat))

# Print category totals sorted by descending total answers:

print()

print("  total   best  category")

for cat in sorted(allcount.keys(), key=lambda c: allcount[c], reverse=True):

    print(" {:6d} {:5d}   {}".format(allcount[cat], besties.get(cat,0), cat))

Updated 6 days ago:

PS: If anyone wants more explanations, the obvious Yahoo! email address will reach me.  Be patient.  I don't check that email daily.

Also, any code for answers that I put on Pastebin are still there at pastebin.com/u/husoski.

So long, and thanks for all the fish...

There are no answers yet.
Be the first to answer this question.