通过字典读取int

时间:2017-03-05 19:45:05

标签: python json dictionary typeerror

我的代码遇到了一些麻烦。当我尝试运行它时,我不断收到错误消息,我无法弄清楚原因。

有人可以帮忙吗?

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

2 个答案:

答案 0 :(得分:1)

在从API返回的JSON中,points是一个整数,而不是字符串,因此c['point']是一个整数。所以写下来没有任何意义:

if points in c['point']:

因为c['point']不是您可以搜索的列表或字符串。你应该使用一个简单的相等测试。

def structure_search_by_points(data, points, s=[]):
    points = int(points)
    for c in data:
        if points == c['point']:
            s.append({'Title of the article is' : c.get('title')})
    return s

答案 1 :(得分:1)

以下是您的代码的一些注释:

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=[]):   # using a mutable as an initialiser
                                   # are you sure you know what you're doing?
    if 'hits' in data:
        for t in data['hits']:
            d.append({'title' : t.get('title'), 'point' : t.get('points')})
            build_structure(t,d)   # are you sure you want to call the
                                   # function recursively? (is the key
                                   # "hits" expected to occur in data["hits"]?
    return d

j = build_structure(j_data)
print(j)
word = "Python"
points = "2"
def structure_search_by_title(data, word, s=[]): # again, a mutable initialiser
    for c in data:
        if word in c['title']:
            s.append({'title' : c.get('title')}) # can use c['title'] here since
                                                 # you know 'title' is in c
    return s

def structure_search_by_points(data, points, s=[]): # another mutable
                                                    # initialiser
    for c in data:
        if points in c['point']:      # according to the error message
                                      # c['point'] is a single number
                                      # but 'in' expects something it can
                                      # search through, like a list
                                      # on top of that you points is a string
                                      # so the two cannot be compared directly
            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)