Beautiful Socialism

About 13 months ago, I made some maps of the 2012 election results using only the socialist candidates. If nothing else, I was curious how many votes they’d get if they all voted together instead of having seven or more different candidates. Those maps were made using Illustrator, Photoshop, and data from the  delightfully detailed, completely comprehensive FEC 2012 Election Results (PDF) I tediously transcribed into Excel. Fun, but yuck.

For 2016, I’m using Python and D3 and will probably throw in some MySQL, PHP, and jQuery along with, of course, plain-old JavaScript for some other fun.

I’ve just finished preparing the latest data from the 2016 election. The post and project are called beautifulSocialism because I use BeautifulSoup. Get it? See what I did there?

First, I just saved the 2016 Presidential Election Results page from Politico. After my first few tries (ever) using Beautiful Soup, I reduced just over 6202 lines of code (and they were really long lines) to 103 equally dense lines of code that I could almost use. I can’t express how proud I am of how elegant it is, IMHO, and how proud I am.

Lines 30-33 were a last-minute addition after I noticed that tag contained the hidden treasure of the full party name.

However, there were some whitespace issues I just could not solve and neither Google searches nor StackOverflow provided solutions that worked for me. Also, BeautifulSoup’s encoding shoved some additional unwanted characters into my “final” product.


I spent a lot of time trying many things but solved neither problem. I made it even worse a couple times, though!

Eventually, I surrendered and used Dreamweaver for a relatively few rounds of Find & Replace. First, I used Dreamweaver’s awesome Apply Source Formatting command which made the code pretty but the number of lines ballooned to 2595.

Sadly, correcting the candidates’ names took far more rounds than I expected because they were screwed up in so many different ways. I wanted full names and, since there were many candidates even I was unfamiliar with, I went to my go-to source of presidential candidate information for the last ten years but‘s lack of state-specific ballot information  (in their defense, that’s not the site’s purpose) posed two problems:

  • They give the candidate’s home state but I didn’t know if Smith from whatever state, for example, would be the Smith running in some other state.
  • They give the party the candidate most identifies with but Politico’s results used whatever was on the ballot–often “unaffiliated,” “independent,” or “other.”

So, much to my chagrin, I used Ballot-o-pedia or whatever it’s called. It’s the slowest damn site on the Internet. I hate to admit that it was a huge help and I’m still not providing a link to it. It made me want to throw stuff several times.

After a bunch of manual editing I didn’t expect, I now have this:


Now for some fun DOM manipulation using jQuery to dynamically add some sexy CSS to for the table. Yes, it might be quicker to just do it manually (much like my eventual editing in Dreamweaver) but I prefer learning, even if it takes longer and I make mistakes. Besides, someday, hopefully, I’ll be working with much larger data sets and this knowledge would, of course, pay off.

That’s unrelated to the maps, of course, but there are so many cool things I can do to practice with this data! I’ll also make something that will find and list all the different parties candidates use in different states (just for fun but also) to consolidate them and use in the maps.

Eventually, much of this work will also apply to my politicsPlay project as well.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s