python-TypeError:字符串索引必须是整数.如何解决



我正在使用TF-IDF查询Google+数据,并将数据另存为JSON文件。使用此文件时出现错误。

法典

import json
import nltk
DATA = 'C:/Users/Dung Ring/Desktop/kpdl/107033731246200681024.json'
data = json.loads(open(DATA).read())
QUERY_TERMS = ['SOPA']
activities = [activity['object']['content'].lower().split() 
          for activity in data 
            if activity['object']['content'] != " "]
# TextCollection provides tf, idf, and tf_idf abstractions so 
# that we don't have to maintain/compute them ourselves
tc = nltk.TextCollection(activities)
relevant_activities = []
for idx in range(len(activities)):
    score = 0
    for term in [t.lower() for t in QUERY_TERMS]:
        score += tc.tf_idf(term, activities[idx])
    if score > 0:
         relevant_activities.append({'score': score, 'title': data[idx]['title'],
                          'url': data[idx]['url']})
# Sort by score and display results
relevant_activities = sorted(relevant_activities, key=lambda p: p['score'],    reverse=True)
for activity in relevant_activities:
      print activity['title']
      print 'tLink: %s' % (activity['url'], )
      print 'tScore: %s' % (activity['score'], )
      print

错误信息

Traceback (most recent call last):
  File "ex9.py", line 11, in <module>
    if activity['object']['content']!= ""]
TypeError: string indices must be integers

我使用Python 2.7。

activityactivity['object'] 都是字符串,而不是预期的字典。打印data并检查。

最新更新