Having trouble adding item to a list - Python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
I am new to boto3 and am successfully getting the AWS info I want when using print, but I cannot for some reason append to a list. I can print out and get exactly what I want, but I cannot for the life of me get out into a list. I tried to make an empty dict and update that empty dict which would be preferable but it didn't work either. Got None.
It returns None when trying to add to a list, but wrks perfectly with print().
def vol_snapshots_size(**kwargs):
session = boto3.Session(profile_name='snap')
ec2 = session.client('ec2', region_name=kwargs['region'])
ebs = session.client('ebs', region_name=kwargs['region'])
snapshots = ec2.describe_snapshots(
Filters=[
{
'Name': 'volume-id',
'Values': [
kwargs['volume_id'],
]
}
],
OwnerIds=['self']
)
sorted_snapshots = sorted(snapshots['Snapshots'], key=lambda snap: snap['StartTime'], reverse=True)
snap_list = []
for i in range(len(sorted_snapshots)-1):
snap = sorted_snapshots[i]
diff_size = snapshots_size(ebs, sorted_snapshots[i]["SnapshotId"], sorted_snapshots[i+1]["SnapshotId"])
out = {'date': snap["StartTime"], 'id': snap["SnapshotId"], 'VolumeID': kwargs['volume_id'], 'size': format_bytes(diff_size)}
snap_list.append(out)
return snap_list
EDIT
This works and prints the data. So it is weird I cannot return.
sorted_snapshots = sorted(snapshots['Snapshots'], key=lambda snap: snap['StartTime'], reverse=True)
out = []
for i in range(len(sorted_snapshots)-1):
snap = sorted_snapshots[i]
diff_size = snapshots_size(ebs, sorted_snapshots[i]["SnapshotId"], sorted_snapshots[i+1]["SnapshotId"])
out.append({'date': snap["StartTime"], 'id': snap["SnapshotId"], 'VolumeID': kwargs['volume_id'], 'size': format_bytes(diff_size)})
print(out)
Just wrote this rough test function to interact with the function above
def get_snap_sizes():
try:
snap.vol_snapshots_size(
region='us-east-1',
volume_id='OMITTED',
output='text'
)
except Exception:
pass
print(get_snap_sizes()

Related

Convert Function Output into Dataframe in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am using nsepython library for data extraction. I was extracting lot size for particular symbol from function nse_get_fno_lot_sizes of nsepython library.
I want the output of function nse_get_fno_lot_sizes in dataframe format instead of current format.
Complete Code - https://github.com/aeron7/nsepython/blob/master/nsepython/rahu.py
nse_get_fno_lot_sizes function-
def nse_get_fno_lot_sizes(symbol="all",mode="list"):
url="https://archives.nseindia.com/content/fo/fo_mktlots.csv"
if(mode=="list"):
s=requests.get(url).text
res_dict = {}
for line in s.split('\n'):
if line != '' and re.search(',', line) and (line.casefold().find('symbol') == -1):
(code, name) = [x.strip() for x in line.split(',')[1:3]]
res_dict[code] = int(name)
if(symbol=="all"):
return res_dict
if(symbol!=""):
return res_dict[symbol.upper()]
if(mode=="pandas"):
payload = pd.read_csv(url)
if(symbol=="all"):
return payload
else:
payload = payload[(payload.iloc[:, 1] == symbol.upper())]
return payload
My Code-
print(nse_get_fno_lot_sizes("adaniports","pandas"))
print(type(nse_get_fno_lot_sizes))
Output-
UNDERLYING SYMBOL JUL-22 AUG-22 SEP-22 DEC-22 MAR-23 JUN-23 DEC-23 JUN-24 DEC-24 JUN-25 DEC-25 JUN-26 DEC-26 JUN-27
8 ADANI PORT & SEZ LTD ADANIPORTS 1250 1250 1250
<class 'function'>
I want to convert the output getting from the nse_get_fno_lot_sizes function as dataframe.
Documentation- https://aeron7.github.io/nsepython/documentation/nsetools.html#top-losers-gainers

Why are values ​different from when I declare my tuple? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to understanding what is happening with my code but I couldn't obtain a solution.
Here is my code:
multipolygon = ((
(-58.89198482 -13.38147202, -58.89189251 -13.38147271, -58.89189321 -13.38156309, -58.89198552 -13.3815624,
-58.89207783 -13.38156171, -58.89217014 -13.38156102, -58.89226245 -13.38156032, -58.89235476 -13.38155963,
-58.89244706 -13.38155894, -58.89253937 -13.38155825, -58.89263168 -13.38155756, -58.89263097 -13.38146719,
-58.89253867 -13.38146788, -58.89244636 -13.38146857, -58.89235405 -13.38146926,
-58.89226174 -13.38146995, -58.89216943 -13.38147064, -58.89207712 -13.38147133,
-58.89198482 -13.38147202)
),
(
(-58.89484849 -13.38172171, -58.89484919 -13.38181209, -58.8949415 -13.38181139,
-58.89503381 -13.3818107, -58.89512612 -13.38181001, -58.89521843 -13.38180932,
-58.89521772 -13.38171894, -58.89531003 -13.38171825, -58.89540234 -13.38171756,
-58.89540163 -13.38162718, -58.89540092 -13.38153681, -58.89540022 -13.38144644,
-58.89530791 -13.38144713, -58.8952156 -13.38144782, -58.89512329 -13.38144851,
-58.89503098 -13.3814492, -58.89493868 -13.3814499, -58.89484637 -13.38145059,
-58.89484707 -13.38154096, -58.89484778 -13.38163134, -58.89484849 -13.38172171)
)
)
for poly in multipolygon:
print(poly)
The problem is when I print my multipolygon, the values are changing.
Here is the output:
(-72.27345684, -72.27336522, -72.27345629999999, -72.27354792, -72.27363954, -72.27373116, -72.27382277, -72.27391439, -72.274006, -72.27409762, -72.27418924, -72.27409816, -72.27400655, -72.27391493, -72.27382331, -72.27373169, -72.27364007, -72.27354845, -72.27345684)
(-72.27657020000001, -72.27666128, -72.27675289, -72.27684451, -72.27693613, -72.27702775, -72.27693666, -72.27702828, -72.2771199, -72.27702881, -72.27693773, -72.27684666, -72.27675504, -72.27666342, -72.2765718, -72.27648018000001, -72.27638858, -72.27629696, -72.27638802999999, -72.27647912, -72.27657020000001)
Why this is happening?
It looks like you're missing commas in the tuples which causes some values to be subtracted
From your code:
-58.89484849 -13.38172171
I'm assuming you mean for it to be -58.89484849, -13.38172171?
otherwise -58.89484849 -13.38172171 = 72.2765702 which matches the printed values
It does math since this is what you have inside your data structure
-58.89198482 -13.38147202 = -72.27345684
What is happening is that you are subtracting (-58.89189251 -13.38147271) before printing

Python using multiple conditions inside a loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to use multiple conditions inside an if-else expression which is as:
from datetime import date, timedelta as td, datetime
import holidays
st_dt = '1/1/2017'
en_dt = '1/5/2017'
st_year = datetime.strptime(st_dt, "%m/%d/%Y").year
en_year = datetime.strptime(en_dt, "%m/%d/%Y").year
st_mnth = datetime.strptime(st_dt, "%m/%d/%Y").month
en_mnth = datetime.strptime(en_dt, "%m/%d/%Y").month
st_date = datetime.strptime(st_dt, "%m/%d/%Y").day
en_date = datetime.strptime(en_dt, "%m/%d/%Y").day
d1 = datetime(st_year, st_mnth, st_date, 0, 0, 0)
d2 = datetime(en_year, en_mnth, en_date, 0, 0, 0)
AllHours = []
i = 0
while(d1<=d2):
AllHours.append(d1)
d1 = d1 + td(hours=1)
us_holidays = holidays.UnitedStates()
HolidayWorkingDay = ['H' if x in us_holidays else 'W' for x in AllHours]
HE = [x.hour for x in AllHours]
DayDefn = ['Type1' if (x == 'H' and y>=7 and y<=23) else 'Type2' for x in HolidayWorkingDay and for y in HE]
So, in the above I am trying to make sure that if certain location in the lists HolidayWorkingDay and HE meet certain criteria I give them name 'Type1' else they are 'Type2'
But it fails on the last line because of bad syntax. I am not sure what the right way to write multiple expression is
If you want nested loops you should write last list comprehension like this (without and):
DayDefn = ['Type1' if (x == 'H' and y>=7 and y<=23) else 'Type2'
for x in HolidayWorkingDay for y in HE]

Issue when accessing hash by key [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am doing my first Python project and I have the following code:
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"]
I am getting this error:
for flight in flights["Quotes"]
^
SyntaxError: invalid syntax
It seems like a very basic issue, but I'm unable to figure out what's wrong.
The hash flights has the following content:
{"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}]}
Why can I not access the hash like this flights["Quotes"]?
Lacking a : at the end.
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"]
Kinda simple, sorry i can't be of more use.
But every python block (for, if, while, with) requires a : at the end of the initiating row followed by a indentation on the code that belongs to that block of code, in this case it's for ...: and well yea your if ...:
Also fixed your indentation (it should be 4 spaces or one tab per block of code)

Python JSON KeyError for key that is not missing in object being parsed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm crawling and parsing the JSON data I'm getting from RiotGames LoL API using Python (2.X) and I'm running into an odd error.
I'm loading in the json data and reading the data attr by attr, which goes great until I hit a certain attr which is clearly in the object I'm trying to extract it from but makes Python throw a KeyError as can be seen in the screenshot below.
Here is the codesnippet where the error takes place. As you can see I print the object (for debugging purposes) and then parse all attr, which works fine but throws a KeyError for unknown reasons at the attr 'doubleKills'. Hope you guys can help ^^
def parseJSON(self, jsonDump):
matchDetailDict = dict()
jsonobj = json.loads(jsonDump)
matchId = jsonobj['matchId']
tmpMatch = Match()
tmpMatch.matchID = matchId
tmpMatch.creationDate = jsonobj['matchCreation']
tmpMatch.matchDuration = jsonobj['matchDuration']
for participant, participantId in zip(jsonobj['participants'], jsonobj['participantIdentities']):
stats = participant['stats']
print stats
tmpStats = MatchPlayerStats()
tmpStats.playerID = participantId['player']['summonerId']
tmpStats.matchID = matchId
tmpStats.winner = stats['winner']
tmpStats.kills = stats['kills']
tmpStats.deaths = stats['deaths']
tmpStats.assists = stats['assists']
tmpStats.kda = (tmpStats.kills + tmpStats.assists)*1.0/max(tmpStats.deaths, 0.5)
tmpStats.visionWardsBoughtInGame = stats['visionWardsBoughtInGame']
tmpStats.sightWardsBoughtInGame = stats['sightWardsBoughtInGame']
tmpStats.championID = participant['championId']
tmpStats.doubleKills = participant['doubleKills'] #KeyError here!
tmpStats.firstBloodAssist = participant['firstBloodAssist']
tmpStats.firstBloodKill = participant['firstBloodKill']
tmpStats.killingSprees = participant['killingSprees']
tmpStats.wardsKilled = participant['wardsKilled']
tmpStats.wardsPlaced = participant['wardsPlaced']
tmpStats.unrealKills = participant['unrealKills']
matchDetailDict[tmpStats.playerID] = tmpStats
tmpMatch.playerStats = matchDetailDict
return tmpMatch
It looks as though the JSON on the Terminal you posted is from stats, but you are trying to use the key on participant.
print 'doubleKills' in stats.keys()
Should evaluate to True

Categories