迭代字典以显示不超过数字的值

时间:2017-11-20 22:13:52

标签: python-2.7 dictionary

API调用的数据看起来像底部的内容。

APIkey = 'PB36zotwgisM02kED1vWwvf7BklqCObDGVoyssVE'
APIUrl='https://api.data.gov/regulations/v3/'
docketID=raw_input("Enter Docket ID") => EPA-HQ-OAR-2011-0028
url=APIUrl+'docket.json?api_key='+APIkey+'&docketId='+docketID
response = json.load(urlopen(url))
Max=150

我想像这样迭代它们: 如果docketID小于150,请翻阅字典并打印titlenumberOfCommentsnumberOfComments

​for dic in response:
    if type(dic) is dict:
        if 'numberOfComments'<Max:
            content={
               'docketID':docketID,
               'title': dic['title'],
               'numberOfComments': item['numberOfComments']        
            }

print content
  

错误:名称&#39;内容&#39;未定义

有人可以帮助使这段代码有效吗? :(

response={u'publicationPeriod': {u'value': u'Fall 2012', u'label': u'Publication Period'}, u'timeTables': [{u'action': u'NPRM', u'actionQualifier': u'NPRM', u'date': u'01/10/2012', u'frCitation': u'77 FR 1434'}, {u'action': u'Final Rule', u'actionQualifier': u'Final Rule', u'date': u'08/13/2012', u'frCitation': u'77 FR 48072'}], u'impactsAndEffects': u'None', u'rin': u'2060-AQ70', u'agendaStageOfRulemaking': {u'value': u'Completed Action', u'label': u'Agenda Stage of Rulemaking'}, u'keywords': {u'value': u'CBI, Confidentiality', u'label': u'Keyword(s)'}, u'legalAuthorities': {u'value': u'42 USC 7401 et seq', u'label': u'Legal Authorities'}, u'energyEffects': {u'value': u'No', u'label': u'Energy Effects'}, u'title': u'Proposed Confidentiality Determinations for Eight Subparts of the Mandatory Greenhouse Gas Reporting Rule', u'generic': {u'value': u'HQ-OAR', u'label': u'Location'}, u'internationalImpacts': {u'value': u'No', u'label': u'International Impacts'}, u'agency': u'Environmental Protection Agency', u'smallEntitiesAffected': {u'value': u'No', u'label': u'Small Entities Affected'}, u'ruleMakingStage': u'Completed', u'priority': u'Substantive, Nonsignificant', u'requiresRegulatoryFlexibilityAnalysis': {u'value': u'No', u'label': u'Requires Regulatory Flexibility Analysis'}, u'legalDeadlines': {u'value': u'None', u'label': u'Legal Deadlines'}, u'cfrCitation': u'40 CFR 98', u'federalismImplications': {u'value': u'No', u'label': u'Federalism Implications'}, u'majorRule': {u'value': u'No', u'label': u'Major Rule'}, u'agencyAcronym': u'EPA', u'docketId': u'EPA-HQ-OAR-2011-0028', u'unfundedMandates': {u'value': u'No', u'label': u'Unfunded Mandates'}, u'numberOfComments': 22, u'shortTitle': {u'value': u'Confidentiality Determinations for Eight Subparts of the GHGRP', u'label': u'Short Title'}, u'includedInRegulatoryPlan': {u'value': u'No', u'label': u'Included in Regulatory Plan'}, u'type': {u'value': u'Rulemaking', u'label': u'Type'}, u'governmentLevelsAffected': {u'value': u'Federal', u'label': u'Government Levels Affected'}, u'docketAbstract': u'This action finalized confidentiality determinations for certain data elements in nine subparts under the Mandatory Greenhouse Gas Reporting Rule. In addition, EPA is finalizing amendments to subpart A to defer the reporting deadline of certain data elements in subparts FF and TT until 2013 and to defer the reporting deadline of certain data elements in subpart W until 2015. Lastly, the EPA is finalizing amendments to subpart I regarding the calculation and reporting of emissions from facilities that use best available monitoring methods. This action does not include final confidentiality determinations for data elements in the "Inputs to Emission Equations" data category.', u'contact': [{u'city': u'Washington', u'fax': u'202 343-2202', u'zip': u'20460', u'agencyName': u'DEFAULT', u'firstName': u'Lisa', u'lastName': u'Bacanskas', u'agency': u'AR', u'streetAddress': u'6207J, Washington, DC 20460', u'phone': u'202 343-9758', u'state': u'DC', u'emailAddress': u'Bacanskas.Lisa@epamail.epa.gov'}, {u'city': u'Washington', u'fax': u'202 343-2359', u'zip': u'20460', u'agencyName': u'DEFAULT', u'firstName': u'Barbora', u'lastName': u'Master', u'agency': u'AR', u'streetAddress': u'6207J, Washington, DC 20460', u'phone': u'202 343-9899', u'state': u'DC', u'emailAddress': u'master.barbora@epamail.epa.gov'}]}

1 个答案:

答案 0 :(得分:1)

如果content子句未评估为if,则在if子句中声明true,因此未定义print。只需将if放在​for dic in response: if item['numberOfComments'] < Max: content={ 'docketID':docketID, 'title': dic['title'], 'numberOfComments': item['numberOfComments'] } print content 子句中:

@Repository
public class BrewerRepository {
    @PersistenceContext(name = "vivesPU")
    private EntityManager entityManager;

    public List<Brewer> getAll() {
        return null;
    }
}