使用Python解析Twitter JSON数据中的'user'数据
我正在用Python解析一些Twitter数据,想要从推文的'user'字段中提取信息。下面是我的代码,还有我一直遇到的错误。我的目标是打印出每一行数据集中用户的屏幕名称、粉丝数量和朋友数量。我相信这只是我这边的一个简单问题,但任何帮助都非常感谢。
代码
import json
import sys
def main():
for line in sys.stdin:
line = line.strip()
data = ''
try:
data = json.loads(line)
except ValueError as detail:
continue
if not (isinstance(data, dict)):
## not a dictionary, skip
pass
elif 'delete' in data:
## a delete element, skip for now.
pass
elif 'user' not in data:
## bizarre userless edge case
pass
else:
print "\t".join([
data['user']['screen_name'],
data['user']['followers_count'],
data['user']['friends_count']
])
if __name__ == '__main__':
main()
错误:
Traceback (most recent call last):
File "/home/titan/print_info_FAST.py", line 33, in <module>
main()
File "/home/titan/print_info_FAST.py", line 29, in main
data['user']['friends_count']
TypeError: sequence item 1: expected string or Unicode, int found
1 个回答
0
join方法是用来把字符串连接在一起的,它需要一个字符串的列表,但你给它的列表里有整数。你需要把这些整数转换成字符串。可以试试这样做:
print "\t".join([
data['user']['screen_name'],
str(data['user']['followers_count']),
str(data['user']['friends_count'])
])