Search code examples
pythonxlrddictionarypython-unicode

Accessing unicoded keys with spaces in them in Python 2.7


I am working on a project where I am trying to take Excel files (read in via xlrd) and geocode addresses in them. For this, I am using a list of directories, with each directory entry a separate site.

Something like addressList[0] will result in the following:

{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 & Girls Clubs of America'}

(And I just realized in the spreadsheet, "affiliation" was spelled incorrectly. Meh.)

Now, I know from looking around that keys in Python can have spaces in them, and that this shouldn't be a problem. But entering addressList[0]['Site Phone'] results in a KeyError. In fact, trying to get the value of the 'Hours' key results in a similar KeyError.

Based on a question on Unicode keys, I tried the following:

STRING_DATA = dict([(str(k), v) for k, v in addressList[0].items()])

Which resulted in a dictionary with entries like:

"text:u'Site Name'": u'Tyrell Industries',

This is reasonably okay, except I'm now having to access the value via STRING_DATA["text:u'Site Name'"], which seems like a pain.

Is there a quicker/easier way to use the keys?


Solution

  • text:u'First name ': u'John', is not a valid dict entry.

    the reason you have text: prefixs before keys like text:u'First Name' is, because you're using xlrd cells as dict's keys.

    you should explicitly extract values from cells by using cell.value

    something like:

    new_keys = [k.value for k in addresslist[0]]