代码之家  ›  专栏  ›  技术社区  ›  ambrish dhaka

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

  •  0
  • ambrish dhaka  · 技术社区  · 7 年前

    我有将近1 gb的文件存储了将近2百万条推文。而且,巨大的文件显然会带来一些错误。错误显示为 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"]]
    

    是否可以找到一个解决方案,在该解决方案中,可以跳过发生此类错误的那些行,并继续执行其余的行。

    2 回复  |  直到 7 年前
        1
  •  1
  •   akshat    7 年前

    这里的问题不在于数据中的行,而在于tweet\u数据本身。如果你检查你的tweet\u数据,你会发现还有一个元素是“int”数据类型( 假设您的tweet\u数据是一个字典列表,因为它只需要“dict或dict列表” )。

    您可能希望检查推特数据以删除字典中的其他值。

    我能够用下面的例子重现 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)
    

    输出:

    显示数据帧

    复制错误:

    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'
    

    如何修剪 “tweet\u数据” : 如果您遵循以下更新,则不需要

    归一化之前,请运行以下命令:

    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
    
        2
  •  1
  •   ambrish dhaka    7 年前

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

    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 dataframe的属性错误的可能性。