【问题标题】:skipping Attribute error while importing twitter data into pandas将推特数据导入熊猫时跳过属性错误
【发布时间】:2018-10-05 09:05:41
【问题描述】:

我有将近 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"]]

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

【问题讨论】:

    标签: json pandas twitter attributeerror


    【解决方案1】:

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

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

    我能够通过以下示例重现 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_data” 不需要,如果您关注下面的更新

    在标准化之前,运行以下代码:

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

    更新:(for 循环)

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

    【讨论】:

    • 你能更新你有问题的代码吗,它在 cmets 中有点不可读
    • 我已经更新了代码,请看。我在 jupyter notebook 中运行这些行。
    • 添加 isinstance 签入 for 循环本身。检查更新的答案
    • 是的,我已经添加了这部分并且它有效,它现在显示在上面。
    • 很高兴为您提供帮助,如果这有帮助,您也可以投票吗
    【解决方案2】:

    最终的代码形式如下:

    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
    

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

    【讨论】:

      猜你喜欢
      • 2020-04-05
      • 2018-11-19
      • 2020-09-25
      • 2023-04-01
      • 2018-12-27
      • 2015-06-10
      • 2019-05-12
      • 1970-01-01
      相关资源
      最近更新 更多