2

I am storing the dates of Presidential speeches and each speech's respective filename in a dictionary. The speeches object looks like this:

[<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>,
<a href="/president/obama/speeches/speech-4424">Remarks on Election Night (November 4, 2008)</a>,...]

And end_link looks like:

['/president/obama/speeches/speech-4427', '/president/obama/speeches/speech-4424',...]

Here's my code:

date_dict = {}
for speech in speeches:
    text = speech.get_text(strip=True)
    date = text[text.index("(") + 1:text.rindex(")")]
    end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
    for x in end_link:
        splitlink = x.split("/")
        president = splitlink[2]
        speech_num = splitlink[4]
        filename = "{0}_{1}".format(president,speech_num)
        if 2 == 2:
            f = date_dict['{0} {1}'.format(filename,date)]

I get the proper date output (e.g. August 15, 1999) and filename is fine. Now, I'm just trying to join the two and am getting the following error:

date_dict['{0} {1}'.format(filename,date)]
KeyError: 'obama_speech-4427 August 28, 2008'

I don't really know where to go from here.

blacksite
  • 12,086
  • 10
  • 64
  • 109
  • I want the value to be the date of the speech, and the key to be the filename. I'm going to match up the dates with some other data by joining with filename as the joining key – blacksite Oct 13 '15 at 21:08
  • 1
    You need to be setting the value of the dictionary slot. Dictionary's relate keys to values. Like mailbox numbers relate to what's inside the mailbox. If you want to save filenames, what do you want the key to be? Speech number would probably work. – Caleb Mauer Oct 13 '15 at 21:09
  • The filenames are what's generated by the `for x in end_link`...`filename = "{0}_{1}".format(president,speech_num)`. An example `filename` would be `obama_speech-4427`. Then, I want to pair that with `August 28, 2008`. – blacksite Oct 13 '15 at 21:13
  • 1
    Why do you want a dictionary? Might a list be better? I get what your code does now, but I'm not sure it's going to do what you want. What will you use the dictionary for when you've filled it? – Caleb Mauer Oct 13 '15 at 21:16
  • After I get `filename` and `date` saved (either in a dictionary or list - I don't care as long as the filenames are properly matched to dates (i.e. a `filename` for an Obama speech should not have the date for a Kennedy speech). I thought a dict would be the best approach here. – blacksite Oct 13 '15 at 21:19
  • OK, I updated my example. You want filename as key, then date as value. Objects might be better in this case too. I think at this point you at least should be past your error message. – Caleb Mauer Oct 13 '15 at 21:25
  • setting `href=True` will avoid calling get multiple times – Padraic Cunningham Oct 13 '15 at 21:32

1 Answers1

5

You aren't setting the value at that key to anything, so Python thinks you're trying to read the key instead. The date_dict dictionary is empty.

You need to be setting a value, so something like this:

date_dict[date] = filename

A dictionary has keys and values. To assign to the dictionary, you would do something like this:

date_dict['key'] = value

There is no problem with the join section. '{0} {1}'.format(filename,date) is fine, although you might want an underscore instead of a space. Or maybe a dash if this is going to go on a website.

Related question about KeyError

Edit

Based on our discussion, I think you need to do this:

date_dict = {}
for speech in speeches:
    text = speech.get_text(strip=True)
    date = text[text.index("(") + 1:text.rindex(")")]
    end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
    for x in end_link:
        splitlink = x.split("/")
        president = splitlink[2]
        speech_num = splitlink[4]
        filename = "{0}_{1}".format(president,speech_num)
        if 2 == 2:
            date_dict[filename] = date

# Prints name date for a given file(just an example)
print("File", filname, "recorded on", date_dict[filename]) 
Community
  • 1
  • 1
Caleb Mauer
  • 662
  • 6
  • 11
  • I see your point, but I still get the same error back, even where I assign `date_dict[...` to a variable – blacksite Oct 13 '15 at 21:04
  • 2
    Note that in the general case, converting to a string risks errors that you could avoid by using `tuple` keys. `date_dict[filename, date] = end_link` avoids string formatting overhead, is equally legal, and keeps the components of the key separated so you can use each component separately when iterating, rather than trying to parse the string. You can always format the key later if you need to use it for output. – ShadowRanger Oct 13 '15 at 21:08
  • Noted. Thanks. I'll worry about formatting once I get the date stored properly. I just need to be able to access a speech by `filename` and get that speech's date. – blacksite Oct 13 '15 at 21:20
  • Great. Thanks for the help. As an aside, I got `filename` to output to a dictionary in a previous `for` loop, so I'm going to see if I can get `date` to be assigned as the value in that dictionary. Again, thanks for your help. – blacksite Oct 13 '15 at 21:29