将推特数据导入 pandas 时跳过属性错误

skipping Attribute error while importing twitter data into pandas

我有将近 1 GB 的文件存储了将近 20 万条推文。而且,巨大的文件显然带有一些错误。错误显示为 AttributeError: 'int' object has no attribute 'items'。当我尝试 运行 这段代码时会发生这种情况。

 raw_data_path = input("Enter the path for raw data file: ")
 tweet_data_path = raw_data_path



 tweet_data = []
 tweets_file = open(tweet_data_path, "r", encoding="utf-8")
 for line in tweets_file:
   try:
    tweet = json.loads(line)
    tweet_data.append(tweet)
   except:
    continue


    tweet_data2 = [tweet for tweet in tweet_data if isinstance(tweet, 
   dict)]



   from pandas.io.json import json_normalize    
tweets = json_normalize(tweet_data2)[["text", "lang", "place.country",
                                     "created_at", "coordinates", 
                                     "user.location", "id"]]

是否可以找到解决方案,可以跳过发生此类错误的行并继续处理其余行。

这里的问题不在于数据行,而在于 tweet_data 本身。如果你检查你的 tweet_data,你会发现另一个元素是 'int' 数据类型(假设你的 tweet_data 是一个字典列表,因为它只需要 "dict or list of dicts").

您可能需要检查您的推文数据以删除字典以外的值。

我能够用下面的例子重现 json_normalize document:

工作示例:

from pandas.io.json import json_normalize
data = [{'state': 'Florida',
         'shortname': 'FL',
         'info': {
              'governor': 'Rick Scott'
         },
         'counties': [{'name': 'Dade', 'population': 12345},
                     {'name': 'Broward', 'population': 40000},
                     {'name': 'Palm Beach', 'population': 60000}]},
        {'state': 'Ohio',
         'shortname': 'OH',
         'info': {
              'governor': 'John Kasich'
         },
         'counties': [{'name': 'Summit', 'population': 1234},
                      {'name': 'Cuyahoga', 'population': 1337}]},
       ]
json_normalize(data)

输出:

Displays datarame

重现错误:

from pandas.io.json import json_normalize
data = [{'state': 'Florida',
         'shortname': 'FL',
         'info': {
              'governor': 'Rick Scott'
         },
         'counties': [{'name': 'Dade', 'population': 12345},
                     {'name': 'Broward', 'population': 40000},
                     {'name': 'Palm Beach', 'population': 60000}]},
        {'state': 'Ohio',
         'shortname': 'OH',
         'info': {
              'governor': 'John Kasich'
         },
         'counties': [{'name': 'Summit', 'population': 1234},
                      {'name': 'Cuyahoga', 'population': 1337}]},
       1  # *Added an integer to the list*
       ]
result = json_normalize(data)

错误:

AttributeError: 'int' object has no attribute 'items'

如何p运行e "tweet_data": 不需要,如果你关注下方更新

规范化前,运行如下:

tweet_data = [tweet for tweet in tweet_data if isinstance(tweet, dict)]

更新:(对于 foor 循环)

for line in tweets_file:
    try:
        tweet = json.loads(line)
        if isinstance(tweet, dict): 
            tweet_data.append(tweet)
    except:
        continue

代码的最终形式如下所示:

tweet_data_path = raw_data_path

 tweet_data = []
 tweets_file = open(tweet_data_path, "r", encoding="utf-8")

for line in tweets_file:
   try:
      tweet = json.loads(line)
      if isinstance(tweet, dict): 
         tweet_data.append(tweet)
      except: 
         continue

这消除了所有可能阻碍导入 panda 数据框的属性错误的可能性。