【发布时间】:2014-04-23 20:32:34
【问题描述】:
我正在尝试使用 Python 解析一些 Twitter 数据,以在推文的“用户”字段中提取信息。下面是我的代码,以及我不断收到的错误。我的目标是打印出数据集中每一行的屏幕名称、关注者数量、朋友数量。我确信这对我来说是一个简单的问题,但非常感谢任何帮助。
代码
导入 json 导入系统
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
【问题讨论】: