【发布时间】:2019-08-06 12:32:15
【问题描述】:
我遇到了一些 json 文件(从 Twython / Tweeter API 生成)的问题。
文件如下所示:
[
{
"created_at": "Thu Mar 14 20:24:53 +0000 2019",
"id": 1106290123426140165,
"id_str": "1106290123426140165",
"text": "RT @ALABDULLATIF: n@B_Al3bdullatif \n\u278b\u2026",
"source": "<a href=\"http://twitter.com/download/android\"
rel=\"nofollow\">Twitter for Android</a>",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 1091414851400929286,
"id_str": "1091414851400929286",
"name": "u064a",
"screen_name": "UThbZ4nwsuzAMQm",
"location": null,
"url": null,
"description": null,
"translator_type": "none",
"protected": false,
"verified": false,
"followers_count": 0,
"friends_count": 0,
"listed_count": 0,
"favourites_count": 0,
"statuses_count": 2,
"created_at": "Fri Feb 01 19:15:52 +0000 2019",
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "F5F8FA",
ETC
当我尝试用这个来阅读它时:
fname = "tweets_03.json"
text=[]
retweets=[]
language=[]
followers=[]
with open(fname, 'r') as f:
for line in f:
if not line.isspace():
tweet = json.loads(line)
text.append(tweet.get('text', ''))
retweets.append(tweet.get('retweet_count',''))
language.append(tweet.get('lang',''))
followers.append(tweet.get('followers_count',''))
text=pd.DataFrame(text)
text.columns=['text']
retweets=pd.DataFrame(retweets)
retweets.columns=['retweets']
language=pd.DataFrame(language)
language.columns=['language']
followers=pd.DataFrame(followers)
followers.columns=['followers']
df=pd.concat([text,retweets,language,followers],axis=1)
df.head(5)
我收到以下错误消息:
JSONDecodeError: Expecting value: line 2 column 1 (char 2)
我也试过了:
data = "tweets_03.json"
jdata = json.loads(data)
df = pd.DataFrame(jdata)
这给了我以下错误:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
如果有人能提供帮助,将不胜感激。我想将数据转换为数据框。 谢谢 最好的祝福
【问题讨论】:
-
当我尝试上述方法时(# 使用 json.load() 读取 JSON 数据 file = 'data.json' with open(file) as train_file: dict_train = json.load(train_file) # 转换从字典到数据帧的 json 数据集 train = pd.DataFrame.from_dict(dict_train, orient='index') train.reset_index(level=0, inplace=True) ,我现在收到以下错误消息: JSONDecodeError: Extra data: line 1938第 2 列(字符 79714)
-
...当我检查第 1938 行的内容时,我看到:][
-
@tezzaaa 我已经更新了答案,请看一下
标签: python json dataframe twitter