Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing unicoded keys with spaces in them in Python 2.7
    primarykey
    data
    text
    <p>I am working on a project where I am trying to take Excel files (read in via <code>xlrd</code>) and geocode addresses in them. For this, I am using a list of directories, with each directory entry a separate site.</p> <p>Something like <code>addressList[0]</code> will result in the following:</p> <pre><code>{text:u'First name ': u'John', text:u'Site City': u'Indio', text:u'Site State': u'CA', text:u'Last name': u'Doe', text:u'Site Phone': u'760-555-1234', text:u'Site Zip': u'92201', text:u'Site Address1': u'1313 Mockingbird Lane', text:u'Site Name': u'Tyrell Industries', text:u'Hours': u'Mon-Fri 12:00-1:00', text:u'Affliation': u'Boys &amp; Girls Clubs of America'} </code></pre> <p>(And I just realized in the spreadsheet, "affiliation" was spelled incorrectly. Meh.)</p> <p>Now, I know from looking around that <a href="https://stackoverflow.com/questions/13474957/spaces-in-python-dictionary-keys">keys in Python can have spaces in them</a>, and that this shouldn't be a problem. But entering <code>addressList[0]['Site Phone']</code> results in a KeyError. In fact, trying to get the value of the 'Hours' key results in a similar KeyError.</p> <p>Based on a <a href="https://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str">question on Unicode keys</a>, I tried the following:</p> <pre><code>STRING_DATA = dict([(str(k), v) for k, v in addressList[0].items()]) </code></pre> <p>Which resulted in a dictionary with entries like:</p> <pre><code>"text:u'Site Name'": u'Tyrell Industries', </code></pre> <p>This is reasonably okay, except I'm now having to access the value via <code>STRING_DATA["text:u'Site Name'"]</code>, which seems like a pain.</p> <p>Is there a quicker/easier way to use the keys?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload