1
我有一個30k用戶文件和一個200k評論文件。每一行都是有效的JSON結構,但整個文件不是。遍歷兩個JSON文件來找到匹配
對於每個評論,我想查找相應的用戶並記下用戶的評論總數,這是用戶詞典中存儲的值。
with open('review_file.json') as reviews, open('user_file.json') as users:
for r in reviews: #r == dict
review = json.loads(r)
print "review iteration"
for s in users: #s == dict
print "user iteration"
user = json.loads(s)
if user['user_id'] == review['user_id']:
print review['id']
print user['user_id']
print user['num_review']
我認爲這將遍歷用戶對於每個評論,但它遍歷用戶的第一次審查,然後只在審查文件迭代,不看用戶。
我錯過了什麼嗎?我會期望它遍歷每個評論的用戶文件。有沒有更簡單的方法來處理這個問題?我一直在發現,任何將文件抽象爲幫助類的嘗試都會顯着減慢搜索速度。