【发布时间】: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