Python 获取错误:JSONDecodeError:期望值:第2行第1列(字符1)
这就是我试图运行的代码。 我正在尝试从tweet中提取地理位置数据,这是一个我试图尝试的方法,但我一直得到这个错误&我只是觉得有点绝望Python 获取错误:JSONDecodeError:期望值:第2行第1列(字符1),python,twitter,geolocation,Python,Twitter,Geolocation,这就是我试图运行的代码。 我正在尝试从tweet中提取地理位置数据,这是一个我试图尝试的方法,但我一直得到这个错误&我只是觉得有点绝望 fname = 'Election2020.txt' with open(fname, 'r') as f: #Create dictionary to later be stored as JSON. All data will be included # in the list 'data' users_with_geoda
fname = 'Election2020.txt'
with open(fname, 'r') as f:
#Create dictionary to later be stored as JSON. All data will be included
# in the list 'data'
users_with_geodata = {
"data": []
}
all_users = []
total_tweets = 0
geo_tweets = 0
for line in f:
tweet = json.loads(line)
if tweet['user']['id']:
total_tweets += 1
user_id = tweet['user']['id']
if user_id not in all_users:
all_users.append(user_id)
#Give users some data to find them by. User_id listed separately
# to make iterating this data later easier
user_data = {
"user_id" : tweet['user']['id'],
"features" : {
"name" : tweet['user']['name'],
"id": tweet['user']['id'],
"screen_name": tweet['user']['screen_name'],
"tweets" : 1,
"location": tweet['user']['location'],
}
}
#Iterate through different types of geodata to get the variable primary_geo
if tweet['coordinates']:
user_data["features"]["primary_geo"] = str(tweet['coordinates'][tweet['coordinates'].keys()[1]][1]) + ", " + str(tweet['coordinates'][tweet['coordinates'].keys()[1]][0])
user_data["features"]["geo_type"] = "Tweet coordinates"
elif tweet['place']:
user_data["features"]["primary_geo"] = tweet['place']['full_name'] + ", " + tweet['place']['country']
user_data["features"]["geo_type"] = "Tweet place"
else:
user_data["features"]["primary_geo"] = tweet['user']['location']
user_data["features"]["geo_type"] = "User location"
#Add only tweets with some geo data to .json. Comment this if you want to include all tweets.
if user_data["features"]["primary_geo"]:
users_with_geodata['data'].append(user_data)
geo_tweets += 1
#If user already listed, increase their tweet count
elif user_id in all_users:
for user in users_with_geodata["data"]:
if user_id == user["user_id"]:
user["features"]["tweets"] += 1
#Count the total amount of tweets for those users that had geodata
for user in users_with_geodata["data"]:
geo_tweets = geo_tweets + user["features"]["tweets"]
#Get some aggregated numbers on the data
print ("The file included " + str(len(all_users)) + " unique users who tweeted with or without geo data")
print ("The file included " + str(len(users_with_geodata['data'])) + " unique users who tweeted with geo data, including 'location'")
print ("The users with geo data tweeted " + str(geo_tweets) + " out of the total " + str(total_tweets) + " of tweets.")
# Save data to JSON file
with open('Election_users_geo.json', 'w') as fout:
fout.write(json.dumps(users_with_geodata, indent=4))
这是我收到的错误消息:
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-2-74ca730155aa> in <module>
15 geo_tweets = 0
16 for line in f:
---> 17 tweet = json.loads(line)
18 if tweet['user']['id']:
19 total_tweets += 1
~\anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None and not kw):
--> 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder
~\anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
~\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 2 column 1 (char 1)
---------------------------------------------------------------------------
JSONDecodeError回溯(最近一次调用)
在里面
15条geo_推文=0
16对于f中的行:
--->17 tweet=json.loads(行)
18如果tweet['user']['id']:
19条推文总数+=1条
加载中的~\anaconda3\lib\json\\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuupy(s、cls、object\u hook、parse\u float、parse\u int、parse\u constant、object\u pairs\u hook、**kw)
355 parse_int为无,parse_float为无且
356 parse_常量为None且对象_pairs_hook为None且非kw):
-->357返回默认解码器。解码
358如果cls为无:
359 cls=JSONDecoder
解码中的~\anaconda3\lib\json\decoder.py(self,s,\u w)
335
336 """
-->337 obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
338 end=_w(s,end).end()
339如果结束!=长度:
原始解码中的~\anaconda3\lib\json\decoder.py(self、s、idx)
353 obj,end=自扫描一次(s,idx)
354除了停止迭代作为错误:
-->355将JSONDecodeError(“预期值”,s,err.value)从None提升
356返回obj,结束
JSONDecodeError:应为值:第2行第1列(字符1)
有没有人对如何从Twitter中提取地理位置有任何其他建议?我感到有点绝望,因为这比我预期的要难,而且我有限的编程技能也无助于解决问题。你能给我们展示一下输入文件中的行的示例吗?如果每一行都是独立的,那就有点不寻常了一个完整的json对象。你是指我正在使用的tweet文件吗?我是指
Election2020.txt
文件。问题是每行末尾都有Windows样式的回车,json无法处理。请尝试以二进制模式打开该文件。使用open(fname,'rb')