Yusuf Ning Yusuf Ning - 1 month ago 5
Python Question

How to count the number of a particular entry. python

Im trying to count the "elite" key, the number of entry with "elite" key that is not empty. and then total the number of "elite" user.

the following is the example data set which should have "1" because there is "1" entry out of 3 that have elite that is not an empty list.

{"yelping_since": "2013-07", "votes": {"funny": 1, "useful": 2, "cool": 2}, "review_count": 5, "name": "Kikki", "user_id": "KW35l9DVkPNJCgApafhE8w", "friends": [], "fans": 0, "average_stars": 3.67, "type": "user", "compliments": {}, "elite": [2012]}
{"yelping_since": "2013-10", "votes": {"funny": 0, "useful": 0, "cool": 0}, "review_count": 1, "name": "Cindy", "user_id": "OdajpYB_nAJXNLSNbyMvxg", "friends": [], "fans": 0, "average_stars": 5.0, "type": "user", "compliments": {}, "elite": []}
{"yelping_since": "2013-10", "votes": {"funny": 0, "useful": 0, "cool": 0}, "review_count": 3, "name": "Lawrence", "user_id": "z-0l9wtrlGBSyMlj4BP9Lw", "friends": [], "fans": 0, "average_stars": 3.5, "type": "user", "compliments": {}, "elite": []}


the following is the code that i have tried

import json

def count_number_of_elite():

with open("data/yelp_academic_dataset_user.txt", "r") as input_file:
for line in input_file:
if json.loads(line)["elite"]:
number_of_elite_user += 1

print (number_of_elite_user)

Answer

Since your input data is in json format, I'd use

import json

file = open("data/yelp_academic_dataset_checkin", "r")

for line in file:
    if json.loads(line)["elite"] != []:
        number_of_elite_user += 1
        return number_of_elite_user

Also the return statement should probably not be in the for-loop, since otherwise it'll always return 1.

Along with improvements that @UnholySheep suggested below in comments, the right way of that code would be:

import json

def count_lines():
    number_of_elite_user = 0
    with open("data/yelp_academic_dataset_checkin", "r") as input_file:
        for line in input_file:
            if json.loads(line)["elite"]:
                number_of_elite_user += 1
    return number_of_elite_user

print(count_lines())