按密钥访问哈希时的问题

时间:2016-04-18 15:34:37

标签: python

我正在做我的第一个Python项目,我有以下代码:

    response = urlopen(request)
    flights = response.read()

    text_file = open("Output.txt", "w")
    text_file.write(flights)
    text_file.close()

    minPrice = LARGE_CONSTANT
    for flight in flights["Quotes"]
      if(flight["MinPrice"] < minPrice)
        minPrice = flight["MinPrice"]

我收到此错误:

    for flight in flights["Quotes"]

                                  ^
SyntaxError: invalid syntax

这似乎是一个非常基本的问题,但我无法弄清楚错误是什么。 哈希flights具有以下内容:

{"Quotes":[{"QuoteId":1,"MinPrice":721.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":40074,"DepartureDate":"2016-05-15T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":40074,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-16T13:58:00"},{"QuoteId":2,"MinPrice":490.0,"Direct":false,"OutboundLeg":{"CarrierIds":[835],"OriginId":50290,"DestinationId":42644,"DepartureDate":"2016-05-14T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":42563,"DestinationId":50290,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-17T21:04:00"},{"QuoteId":3,"MinPrice":596.0,"Direct":false,"OutboundLeg":{"CarrierIds":[835],"OriginId":50290,"DestinationId":42644,"DepartureDate":"2016-05-28T00:00:00"},"InboundLeg":{"CarrierIds":[1710],"OriginId":42644,"DestinationId":50290,"DepartureDate":"2016-05-29T00:00:00"},"QuoteDateTime":"2016-04-07T22:10:00"},{"QuoteId":4,"MinPrice":574.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":42664,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":42664,"DestinationId":60987,"DepartureDate":"2016-05-08T00:00:00"},"QuoteDateTime":"2016-04-13T02:09:00"},{"QuoteId":5,"MinPrice":856.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":42664,"DepartureDate":"2016-05-05T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":42664,"DestinationId":50290,"DepartureDate":"2016-05-10T00:00:00"},"QuoteDateTime":"2016-04-15T23:11:00"},{"QuoteId":6,"MinPrice":741.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1033],"OriginId":60987,"DestinationId":43139,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1033],"OriginId":43139,"DestinationId":60987,"DepartureDate":"2016-05-16T00:00:00"},"QuoteDateTime":"2016-04-15T16:28:00"},{"QuoteId":7,"MinPrice":729.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":65633,"DestinationId":45676,"DepartureDate":"2016-05-07T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":45676,"DestinationId":60987,"DepartureDate":"2016-05-14T00:00:00"},"QuoteDateTime":"2016-04-07T05:58:00"},{"QuoteId":8,"MinPrice":848.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1033],"OriginId":60987,"DestinationId":47777,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1033],"OriginId":47777,"DestinationId":60987,"DepartureDate":"2016-05-16T00:00:00"},"QuoteDateTime":"2016-04-09T10:48:00"},{"QuoteId":9,"MinPrice":804.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":49369,"DepartureDate":"2016-05-27T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":49369,"DestinationId":50290,"DepartureDate":"2016-05-30T00:00:00"},"QuoteDateTime":"2016-04-18T09:39:00"},{"QuoteId":10,"MinPrice":576.0,"Direct":false,"OutboundLeg":{"CarrierIds":[838],"OriginId":50290,"DestinationId":49369,"DepartureDate":"2016-05-03T00:00:00"},"InboundLeg":{"CarrierIds":[838],"OriginId":49369,"DestinationId":60987,"DepartureDate":"2016-05-05T00:00:00"},"QuoteDateTime":"2016-04-09T15:12:00"},{"QuoteId":11,"MinPrice":726.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":49793,"DepartureDate":"2016-05-17T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":49793,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-17T21:30:00"},{"QuoteId":12,"MinPrice":1172.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":50340,"DepartureDate":"2016-05-01T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":50340,"DestinationId":60987,"DepartureDate":"2016-05-04T00:00:00"},"QuoteDateTime":"2016-04-12T15:01:00"},{"QuoteId":13,"MinPrice":666.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1907],"OriginId":65633,"DestinationId":54353,"DepartureDate":"2016-05-10T00:00:00"},"InboundLeg":{"CarrierIds":[1907],"OriginId":54353,"DestinationId":65633,"DepartureDate":"2016-05-21T00:00:00"},"QuoteDateTime":"2016-04-04T01:14:00"},{"QuoteId":14,"MinPrice":802.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":54353,"DepartureDate":"2016-05-18T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":54353,"DestinationId":50290,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-18T01:59:00"},{"QuoteId":15,"MinPrice":754.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":59078,"DepartureDate":"2016-05-15T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":59078,"DestinationId":60987,"DepartureDate":"2016-05-22T00:00:00"},"QuoteDateTime":"2016-04-15T01:55:00"},{"QuoteId":16,"MinPrice":1375.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":50290,"DestinationId":59117,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":59117,"DestinationId":50290,"DepartureDate":"2016-05-13T00:00:00"},"QuoteDateTime":"2016-04-06T09:28:00"},{"QuoteId":17,"MinPrice":893.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":50290,"DestinationId":60946,"DepartureDate":"2016-05-10T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":60946,"DestinationId":60987,"DepartureDate":"2016-05-17T00:00:00"},"QuoteDateTime":"2016-04-10T16:56:00"},{"QuoteId":18,"MinPrice":735.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":65393,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":65393,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-16T15:37:00"},{"QuoteId":19,"MinPrice":520.0,"Direct":false,"OutboundLeg":{"CarrierIds":[858],"OriginId":60987,"DestinationId":65465,"DepartureDate":"2016-05-21T00:00:00"},"InboundLeg":{"CarrierIds":[858],"OriginId":65698,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-09T18:44:00"},{"QuoteId":20,"MinPrice":538.0,"Direct":false,"OutboundLeg":{"CarrierIds":[858],"OriginId":60987,"DestinationId":65465,"DepartureDate":"2016-05-01T00:00:00"},"InboundLeg":{"CarrierIds":[858],"OriginId":65465,"DestinationId":60987,"DepartureDate":"2016-05-03T00:00:00"},"QuoteDateTime":"2016-04-17T17:29:00"},{"QuoteId":21,"MinPrice":602.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1081],"OriginId":60987,"DestinationId":65655,"DepartureDate":"2016-05-03T00:00:00"},"InboundLeg":{"CarrierIds":[1081],"OriginId":65655,"DestinationId":50290,"DepartureDate":"2016-05-12T00:00:00"},"QuoteDateTime":"2016-04-06T19:36:00"},{"QuoteId":22,"MinPrice":580.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1001],"OriginId":60987,"DestinationId":65655,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[1001],"OriginId":65655,"DestinationId":60987,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-12T06:36:00"},{"QuoteId":23,"MinPrice":494.0,"Direct":false,"OutboundLeg":{"CarrierIds":[835],"OriginId":50290,"DestinationId":65698,"DepartureDate":"2016-05-12T00:00:00"},"InboundLeg":{"CarrierIds":[835],"OriginId":65698,"DestinationId":65633,"DepartureDate":"2016-05-14T00:00:00"},"QuoteDateTime":"2016-04-17T00:26:00"},{"QuoteId":24,"MinPrice":666.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1859],"OriginId":60987,"DestinationId":65698,"DepartureDate":"2016-05-05T00:00:00"},"InboundLeg":{"CarrierIds":[838],"OriginId":65698,"DestinationId":60987,"DepartureDate":"2016-05-17T00:00:00"},"QuoteDateTime":"2016-04-07T12:21:00"},{"QuoteId":25,"MinPrice":733.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1033],"OriginId":60987,"DestinationId":66076,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1033],"OriginId":66076,"DestinationId":60987,"DepartureDate":"2016-05-16T00:00:00"},"QuoteDateTime":"2016-04-09T06:06:00"},{"QuoteId":26,"MinPrice":5673.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1385],"OriginId":60987,"DestinationId":66270,"DepartureDate":"2016-05-03T00:00:00"},"InboundLeg":{"CarrierIds":[1385],"OriginId":66270,"DestinationId":60987,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-13T20:36:00"},{"QuoteId":27,"MinPrice":1379.0,"Direct":true,"OutboundLeg":{"CarrierIds":[2058],"OriginId":50290,"DestinationId":66270,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[2058],"OriginId":66270,"DestinationId":50290,"DepartureDate":"2016-05-21T00:00:00"},"QuoteDateTime":"2016-04-13T04:57:00"},{"QuoteId":28,"MinPrice":505.0,"Direct":true,"OutboundLeg":{"CarrierIds":[105],"OriginId":60987,"DestinationId":67662,"DepartureDate":"2016-05-12T00:00:00"},"InboundLeg":{"CarrierIds":[105],"OriginId":67662,"DestinationId":60987,"DepartureDate":"2016-05-20T00:00:00"},"QuoteDateTime":"2016-04-13T10:12:00"},{"QuoteId":29,"MinPrice":551.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":67662,"DepartureDate":"2016-05-20T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":67662,"DestinationId":60987,"DepartureDate":"2016-05-22T00:00:00"},"QuoteDateTime":"2016-04-12T02:42:00"},{"QuoteId":30,"MinPrice":906.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":50290,"DestinationId":68229,"DepartureDate":"2016-05-17T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":68229,"DestinationId":50290,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-17T15:23:00"},{"QuoteId":31,"MinPrice":804.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":70060,"DepartureDate":"2016-05-27T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":70060,"DestinationId":50290,"DepartureDate":"2016-05-31T00:00:00"},"QuoteDateTime":"2016-04-18T14:38:00"},{"QuoteId":32,"MinPrice":618.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":70060,"DepartureDate":"2016-05-19T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":70060,"DestinationId":60987,"DepartureDate":"2016-05-22T00:00:00"},"QuoteDateTime":"2016-04-09T17:33:00"},{"QuoteId":33,"MinPrice":823.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":70745,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":70745,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-16T21:40:00"},{"QuoteId":34,"MinPrice":1129.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1324],"OriginId":60987,"DestinationId":71017,"DepartureDate":"2016-05-23T00:00:00"},"InboundLeg":{"CarrierIds":[1324],"OriginId":71017,"DestinationId":60987,"DepartureDate":"2016-05-27T00:00:00"},"QuoteDateTime":"2016-04-03T21:25:00"},{"QuoteId":35,"MinPrice":798.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":82165,"DepartureDate":"2016-05-10T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":82165,"DestinationId":60987,"DepartureDate":"2016-05-17T00:00:00"},"QuoteDateTime":"2016-04-10T03:23:00"},{"QuoteId":36,"MinPrice":726.0,"Direct":false,"OutboundLeg":{"CarrierIds":[1523],"OriginId":60987,"DestinationId":82398,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[1523],"OriginId":82398,"DestinationId":60987,"DepartureDate":"2016-05-14T00:00:00"},"QuoteDateTime":"2016-04-16T23:32:00"},{"QuoteId":37,"MinPrice":475.0,"Direct":true,"OutboundLeg":{"CarrierIds":[1368],"OriginId":50290,"DestinationId":42563,"DepartureDate":"2016-05-18T00:00:00"},"InboundLeg":{"CarrierIds":[1368],"OriginId":42563,"DestinationId":50290,"DepartureDate":"2016-05-25T00:00:00"},"QuoteDateTime":"2016-04-18T02:26:00"},{"QuoteId":38,"MinPrice":795.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":63721,"DepartureDate":"2016-05-04T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":63721,"DestinationId":60987,"DepartureDate":"2016-05-09T00:00:00"},"QuoteDateTime":"2016-04-17T18:04:00"},{"QuoteId":39,"MinPrice":799.0,"Direct":false,"OutboundLeg":{"CarrierIds":[881],"OriginId":60987,"DestinationId":66217,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[881],"OriginId":66217,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-10T19:24:00"},{"QuoteId":40,"MinPrice":819.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":82649,"DepartureDate":"2016-05-16T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":82649,"DestinationId":60987,"DepartureDate":"2016-05-23T00:00:00"},"QuoteDateTime":"2016-04-08T13:48:00"},{"QuoteId":41,"MinPrice":773.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":65633,"DestinationId":44620,"DepartureDate":"2016-05-09T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":44620,"DestinationId":65633,"DepartureDate":"2016-05-11T00:00:00"},"QuoteDateTime":"2016-04-08T22:21:00"},{"QuoteId":42,"MinPrice":995.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":47540,"DepartureDate":"2016-05-15T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":47540,"DestinationId":60987,"DepartureDate":"2016-05-24T00:00:00"},"QuoteDateTime":"2016-04-16T13:57:00"},{"QuoteId":43,"MinPrice":6835.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":42843,"DepartureDate":"2016-05-06T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":42843,"DestinationId":60987,"DepartureDate":"2016-05-21T00:00:00"},"QuoteDateTime":"2016-04-17T19:06:00"},{"QuoteId":44,"MinPrice":6826.0,"Direct":false,"OutboundLeg":{"CarrierIds":[],"OriginId":60987,"DestinationId":54367,"DepartureDate":"2016-05-05T00:00:00"},"InboundLeg":{"CarrierIds":[],"OriginId":54367,"DestinationId":60987,"DepartureDate":"2016-05-11T00:00:00"},"QuoteDateTime":"2016-04-13T09:33:00"}],"Places":[{"PlaceId":40074,"IataCode":"ABZ","Name":"Aberdeen","Type":"Station","CityName":"Aberdeen","CityId":"ABER","CountryName":"United Kingdom"},{"PlaceId":42563,"IataCode":"BFS","Name":"Belfast International","Type":"Station","CityName":"Belfast","CityId":"BELF","CountryName":"United Kingdom"},{"PlaceId":42644,"IataCode":"BHD","Name":"Belfast City","Type":"Station","CityName":"Belfast","CityId":"BELF","CountryName":"United Kingdom"},{"PlaceId":42664,"IataCode":"BHX","Name":"Birmingham","Type":"Station","CityName":"Birmingham","CityId":"BIRM","CountryName":"United Kingdom"},{"PlaceId":42843,"IataCode":"BLK","Name":"Blackpool","Type":"Station","CityName":"Blackpool","CityId":"BLAC","CountryName":"United Kingdom"},{"PlaceId":43139,"IataCode":"BRS","Name":"Bristol","Type":"Station","CityName":"Bristol","CityId":"BRIS","CountryName":"United Kingdom"},{"PlaceId":44620,"IataCode":"CAL","Name":"Campbeltown","Type":"Station","CityName":"Campbeltown","CityId":"CAMP","CountryName":"United Kingdom"},{"PlaceId":45676,"IataCode":"CWL","Name":"Cardiff","Type":"Station","CityName":"Cardiff","CityId":"CARD","CountryName":"United Kingdom"},{"PlaceId":47540,"IataCode":"DND","Name":"Dundee","Type":"Station","CityName":"Dundee","CityId":"DUND","CountryName":"United Kingdom"},{"PlaceId":47777,"IataCode":"DSA","Name":"Doncaster Sheffield","Type":"Station","CityName":"Doncaster","CityId":"DONC","CountryName":"United Kingdom"},{"PlaceId":49369,"IataCode":"EDI","Name":"Edinburgh","Type":"Station","CityName":"Edinburgh","CityId":"EDIN","CountryName":"United Kingdom"},{"PlaceId":49793,"IataCode":"EMA","Name":"East Midlands","Type":"Station","CityName":"Nottingham","CityId":"NOTT","CountryName":"United Kingdom"},{"PlaceId":50290,"IataCode":"EWR","Name":"New York Newark","Type":"Station","CityName":"New York","CityId":"NYCA","CountryName":"United States"},{"PlaceId":50340,"IataCode":"EXT","Name":"Exeter","Type":"Station","CityName":"Exeter","CityId":"EXET","CountryName":"United Kingdom"},{"PlaceId":54353,"IataCode":"GLA","Name":"Glasgow International","Type":"Station","CityName":"Glasgow","CityId":"GLAS","CountryName":"United Kingdom"},{"PlaceId":54367,"IataCode":"GLO","Name":"Gloucestershire","Type":"Station","CityName":"Gloucester","CityId":"GLOA","CountryName":"United Kingdom"},{"PlaceId":57113,"IataCode":"HUY","Name":"Humberside","Type":"Station","CityName":"Humberside","CityId":"HUMB","CountryName":"United Kingdom"},{"PlaceId":59078,"IataCode":"INV","Name":"Inverness","Type":"Station","CityName":"Inverness","CityId":"INVE","CountryName":"United Kingdom"},{"PlaceId":59117,"IataCode":"IOM","Name":"Ronaldsway","Type":"Station","CityName":"Castletown","CityId":"CAST","CountryName":"United Kingdom"},{"PlaceId":60946,"IataCode":"JER","Name":"Jersey","Type":"Station","CityName":"Jersey","CityId":"JERS","CountryName":"United Kingdom"},{"PlaceId":60987,"IataCode":"JFK","Name":"New York John F. Kennedy","Type":"Station","CityName":"New York","CityId":"NYCA","CountryName":"United States"},{"PlaceId":63721,"IataCode":"KOI","Name":"Orkney Kirkwall","Type":"Station","CityName":"Orkney","CityId":"ORKN","CountryName":"United Kingdom"},{"PlaceId":65393,"IataCode":"LBA","Name":"Leeds Bradford","Type":"Station","CityName":"Leeds","CityId":"LEED","CountryName":"United Kingdom"},{"PlaceId":65465,"IataCode":"LCY","Name":"London City","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":65633,"IataCode":"LGA","Name":"New York La Guardia","Type":"Station","CityName":"New York","CityId":"NYCA","CountryName":"United States"},{"PlaceId":65655,"IataCode":"LGW","Name":"London Gatwick","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":65698,"IataCode":"LHR","Name":"London Heathrow","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":66076,"IataCode":"LPL","Name":"Liverpool","Type":"Station","CityName":"Liverpool","CityId":"LIVE","CountryName":"United Kingdom"},{"PlaceId":66217,"IataCode":"LSI","Name":"Sumburgh Shetlands","Type":"Station","CityName":"Sumburgh","CityId":"SUMB","CountryName":"United Kingdom"},{"PlaceId":66270,"IataCode":"LTN","Name":"London Luton","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":67662,"IataCode":"MAN","Name":"Manchester","Type":"Station","CityName":"Manchester","CityId":"MANC","CountryName":"United Kingdom"},{"PlaceId":68229,"IataCode":"MME","Name":"Durham Tees Valley","Type":"Station","CityName":"Durham","CityId":"MMEA","CountryName":"United Kingdom"},{"PlaceId":70060,"IataCode":"NCL","Name":"Newcastle","Type":"Station","CityName":"Newcastle","CityId":"NEWC","CountryName":"United Kingdom"},{"PlaceId":70745,"IataCode":"NQY","Name":"Newquay","Type":"Station","CityName":"Newquay","CityId":"NEWQ","CountryName":"United Kingdom"},{"PlaceId":71017,"IataCode":"NWI","Name":"Norwich","Type":"Station","CityName":"Norwich","CityId":"NORW","CountryName":"United Kingdom"},{"PlaceId":82165,"IataCode":"SOU","Name":"Southampton","Type":"Station","CityName":"Southampton","CityId":"SOUT","CountryName":"United Kingdom"},{"PlaceId":82398,"IataCode":"STN","Name":"London Stansted","Type":"Station","CityName":"London","CityId":"LOND","CountryName":"United Kingdom"},{"PlaceId":82649,"IataCode":"SYY","Name":"Stornoway","Type":"Station","CityName":"Stornoway","CityId":"STOR","CountryName":"United Kingdom"},{"PlaceId":91075,"IataCode":"WIC","Name":"Wick","Type":"Station","CityName":"Wick","CityId":"WICK","CountryName":"United Kingdom"},{"PlaceId":3413153,"IataCode":"NYC","Name":"New York","Type":"City","CityName":"New York","CityId":"NYCA"}],"Carriers":[{"CarrierId":105,"Name":"Thomas Cook Airlines"},{"CarrierId":835,"Name":"Air Canada"},{"CarrierId":838,"Name":"Air France"},{"CarrierId":858,"Name":"Alitalia"},{"CarrierId":881,"Name":"British Airways"},{"CarrierId":1001,"Name":"Norwegian"},{"CarrierId":1033,"Name":"Aer Lingus"},{"CarrierId":1081,"Name":"Icelandair"},{"CarrierId":1324,"Name":"KLM"},{"CarrierId":1368,"Name":"Lufthansa"},{"CarrierId":1385,"Name":"EL AL Israel Airlines"},{"CarrierId":1523,"Name":"Austrian Airlines"},{"CarrierId":1710,"Name":"Brussels Airlines"},{"CarrierId":1859,"Name":"Virgin Atlantic"},{"CarrierId":1907,"Name":"WestJet"},{"CarrierId":2058,"Name":"La Compagnie"}],"Currencies":[{"Code":"USD","Symbol":"$","ThousandsSeparator":",","DecimalSeparator":".","SymbolOnLeft":true,"SpaceBetweenAmountAndSymbol":false,"RoundingCoefficient":0,"DecimalDigits":2}]}

为什么我不能像flights["Quotes"]那样访问哈希?

1 个答案:

答案 0 :(得分:2)

最后缺少:

response = urlopen(request)
flights = response.read()

text_file = open("Output.txt", "w")
text_file.write(flights)
text_file.close()

minPrice = LARGE_CONSTANT
for flight in flights["Quotes"]:
   if(flight["MinPrice"] < minPrice):
       minPrice = flight["MinPrice"]

有点简单,抱歉我不能使用。
但是每个python块(for,if,while,with)在起始行的末尾需要一个:,然后在属于该代码块的代码上缩进,在这种情况下它是for ...:以及你的if ...:

还修复了缩进(每个代码块应该是4个空格或一个制表符)