2017-11-20 7 views
0

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 

私はそうのようにそれらを反復処理したい:誰かが助けることができる辞書通じ 反復とnumberOfCommentsは150

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

print content 

error: name 'content' is not defined

よりも小さい場合docketIDtitlenumberOfCommentsを印刷このコードを動作させるには? :(


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'[email protected]'}, {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'[email protected]'}]} 

答えて

1

if句がtrueに評価されない場合は単にif句内printを置くあなたは、IF-句内contentを宣言し、それがため、定義されていません:。

​for dic in response: 
    if item['numberOfComments'] < Max: 
     content={ 
      'docketID':docketID, 
      'title': dic['title'], 
      'numberOfComments': item['numberOfComments'] 
     }   
     print content 
+1

私はすべてのコメントが無関係であり、それらを消去していることが判明しました。 のkey、response.items()の値:type(value)== int:print value' 、bc ['numberOfComments']は辞書内の唯一の_int_であることがあります。ありがとうございました! – ksmyung1

関連する問題