keyerror

What is the pythonic way to access nested dicts without NoneType errors

喜你入骨 提交于 2019-12-07 20:54:03
问题 I have a deep nested dict (decoded from json, from the instagram api). My initial code was like this: caption = post['caption']['text'] But that would throw a NoneType or KeyError error if the 'caption' key or the 'text' key doesn't exist. So I came up with this: caption = post.get('caption', {}).get("text") Which works, but I'm not sure about the style of it. For instance, if I apply this technique to one of the deeper nested attributes I'm trying to retrieve, it looks pretty ugly: image_url

python dictionary datetime as key, keyError

拜拜、爱过 提交于 2019-12-06 20:21:09
问题 I'm trying to run a Python script using cron in Linux, which should construct a dictionary of data. I'm attempting to use datetime().now().time() as keys in the dictionary, but it seems to raise an error. Can't the datetime type be used as a dictionary key in Python? If that is the case, what are my alternatives? Code: time_now = dt.datetime.now().time() date_today = dt.datetime.now().date() usage_dict_hourly = {} date_wise_dict = {} def constructing_dict(data_int): date_wise_dict[usage_dict

KeyError: 'plotly_domain' when using plotly to do scatter plot in python

元气小坏坏 提交于 2019-12-06 16:36:45
问题 I'm using plotly to do scatter plot. The graph is generated on my account but the terminal still reports an error: Traceback (most recent call last): File "IEORE4709HW1.py", line 106, in <module> py.iplot(data, filename='basic-scatter') File "/Library/Python/2.7/site-packages/plotly/plotly/plotly.py", line 175, in iplot return tools.embed(url, **embed_options) File "/Library/Python/2.7/site-packages/plotly/tools.py", line 412, in embed != session.get_session_config()['plotly_domain']):

What is the pythonic way to access nested dicts without NoneType errors

删除回忆录丶 提交于 2019-12-06 13:55:24
I have a deep nested dict (decoded from json, from the instagram api). My initial code was like this: caption = post['caption']['text'] But that would throw a NoneType or KeyError error if the 'caption' key or the 'text' key doesn't exist. So I came up with this: caption = post.get('caption', {}).get("text") Which works, but I'm not sure about the style of it. For instance, if I apply this technique to one of the deeper nested attributes I'm trying to retrieve, it looks pretty ugly: image_url = post.get('images',{}).get('standard_resolution',{}).get('url') Is there a better, more pythonic, way

Python 3 Selenium KeyError: 'value' Issue won't initialize Geckodriver for Firefox

旧街凉风 提交于 2019-12-06 07:57:01
问题 I'm having trouble running geckodriver with Python 3. I recently switched to Python 3 with an application that I've been working on, and have updated Firefox(53.0), Selenium(3.4.3), and geckodriver(0.17.1). I'm also using OSX and used pip to install all of my packages. from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary # Set Firefox Settings # binary = FirefoxBinary('Users/username/Applications/Firefox.app/Contents/MacOS/firefox') # binary =

Python email bot Pyzmail/IMAPclient error

会有一股神秘感。 提交于 2019-12-06 07:52:38
问题 So I'm working on a Python script to extract text from an email and following these instructions to do so. This is the script thus far: import imapclient import pprint import pyzmail mymail = "my@email.com" password = input("Password: ") imapObj = imapclient.IMAPClient('imap.gmail.com' , ssl=True) imapObj.login(mymail , password) imapObj.select_folder('INBOX', readonly=False) UIDs = imapObj.search(['SUBJECT Testing']) rawMessages = imapObj.fetch([5484], ['BODY[]']) message = pyzmail

Getting KeyError in my django code

此生再无相见时 提交于 2019-12-06 00:30:28
I'm new to both Django and Python so please forgive me if I come off as annoying....I'm just very much misinformed! Error Code: http://i.gyazo.com/68d88cabf536b129dc37cde6c3ae319c.png I've googled about this 'KeyError' and it seems to be related to clean(). However, the example my lecturer gave me worked ok without it but when I tried to recreate what he gave me I kept getting this error. A bit of info: I had originally had a ForeignKey for the user for each submission so I changed it to a simple form to fill in (not a permanent solution) but I still get a KeyError. Here is my models, views

Older versions of spaCy throws “KeyError: 'package'” error when trying to install a model

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 20:35:27
I use spaCy 1.6.0 on Ubuntu 14.04.4 LTS x64 with python3.5. To install the English model of spaCy, I tried to run: This gives me the error message: ubun@ner-3:~/NeuroNER-master/src$ python3.5 -m spacy.en.download Downloading parsing model Traceback (most recent call last): File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.5/runpy.py", line 85, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.5/dist-packages/spacy/en/download.py", line 25, in <module> plac.call(main) File "/usr/local/lib/python3.5/dist-packages

Keras GRU NN KeyError when fitting : “not in index”

醉酒当歌 提交于 2019-12-05 07:15:34
I'm currently facing an issue while trying to fit my GRU model with my training data. After a quick look on StackOverflow, I found this post to be quite similar to my issue : Simplest Lstm training with Keras io My own model is as follow : nn = Sequential() nn.add(Embedding(input_size, hidden_size)) nn.add(GRU(hidden_size_2, return_sequences=False)) nn.add(Dropout(0.2)) nn.add(Dense(output_size)) nn.add(Activation('linear')) nn.compile(loss='mse', optimizer="rmsprop") history = History() nn.fit(X_train, y_train, batch_size=30, nb_epoch=200, validation_split=0.1, callbacks=[history]) And the

python dictionary datetime as key, keyError

爱⌒轻易说出口 提交于 2019-12-05 02:51:48
I'm trying to run a Python script using cron in Linux, which should construct a dictionary of data. I'm attempting to use datetime().now().time() as keys in the dictionary, but it seems to raise an error. Can't the datetime type be used as a dictionary key in Python? If that is the case, what are my alternatives? Code: time_now = dt.datetime.now().time() date_today = dt.datetime.now().date() usage_dict_hourly = {} date_wise_dict = {} def constructing_dict(data_int): date_wise_dict[usage_dict_hourly[time_now]] = data_int print date_wise_dict Error: <ipython-input-9-ef6a500cc71b> in constructing