【发布时间】:2017-07-25 14:01:34
【问题描述】:
我的代码有些问题。当我尝试运行它时,我不断收到一条错误消息,我不知道为什么。
有人可以帮忙吗?
import requests
import argparse
import json
from urllib.request import urlopen
url = "http://hn.algolia.com/api/v1/search_by_date?tags=story&numericFilters=created_at_i>1488196800,created_at_i<1488715200"
r = urlopen(url)
data = r.read().decode("utf-8")
j_data = json.loads(data)
def build_structure(data, d=[]):
if 'hits' in data:
for t in data['hits']:
d.append({'title' : t.get('title'), 'point' : t.get('points')})
build_structure(t,d)
return d
j = build_structure(j_data)
print(j)
word = "Python"
points = "2"
def structure_search_by_title(data, word, s=[]):
for c in data:
if word in c['title']:
s.append({'title' : c.get('title')})
return s
def structure_search_by_points(data, points, s=[]):
for c in data:
if points in c['point']:
s.append({'Title of the article is' : c.get('title')})
k = structure_search_by_title(j, word)
l = structure_search_by_points(j, points)
print(k)
print(l)
这是我遇到的错误
File "C:/Users/PycharmProjects/Project1/Project1.py", line 36, in <module>
l = structure_search_by_points(j, points)
File "C:/Users/PycharmProjects/Project1/Project1.py", line 31, in structure_search_by_points
if points in c['point']:
TypeError: argument of type 'int' is not iterable
【问题讨论】:
-
你应该给你的变量起更有意义的名字。它使调试程序变得更加容易。
标签: python json dictionary typeerror