我试图计算"精英" key," elite"键不是空的。然后总计"精英"用户。
以下是应该具有" 1"的示例数据集。因为有" 1" 3名精英中没有空名单的人。
{"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": []}
以下是我试过的代码
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)
答案 0 :(得分:2)
由于您的输入数据是json格式,我使用
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
此外,return语句可能不应该在for循环中,否则它将始终返回1.
除了@UnholySheep在评论中提出的改进之外,该代码的正确方法是:
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())
答案 1 :(得分:1)
下面的代码执行您想要的操作:
首先确保导入json:
import json
并将函数体改为:
file = open("data/yelp_academic_dataset_checkin", "r")
number_of_elite_user = 0
for line in file:
user = json.loads(line)
if (len(user['elite'])>0):
number_of_elite_user += 1
return number_of_elite_user