{"id":229,"date":"2008-04-04T00:36:39","date_gmt":"2008-04-04T00:36:39","guid":{"rendered":"http:\/\/www.sasamat.ca\/test\/?p=229"},"modified":"2014-02-10T00:05:05","modified_gmt":"2014-02-10T00:05:05","slug":"code","status":"publish","type":"post","link":"https:\/\/www.timwyatt.ca\/test\/?p=229","title":{"rendered":"code"},"content":{"rendered":"<p>Use a dictionary to count the number of hits by hostname from a proxy log file, where the 7th tab delimited entry per line is the host. KeyError is the exception thrown if a dictionary doesn&#8217;t yet contain the key being referred to.<\/p>\n<pre>def parselog(infile):\n  i = 0\n  fbook = 0\n  totals = {}\n\n  for line in infile.xreadlines():\n    desthost = line.split(\"\\t\")[6]\n \n    try:\n      totals[desthost] += 1\n    except KeyError:\n      totals[desthost] = 1\n\n    if desthost.find(\"facebook\") &gt; 0:\n      fbook +=1\n\n    i += 1\n \n    return i, fbook, totals<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Use a dictionary to count the number of hits by hostname from a proxy log file, where the 7th tab delimited entry per line is the host. KeyError is the exception thrown if a dictionary&hellip;<\/p>\n<p><a class=\"readmore\" href=\"https:\/\/www.timwyatt.ca\/test\/?p=229\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-229","post","type-post","status-publish","format-standard","hentry","category-uncategorized","comments-off"],"_links":{"self":[{"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=\/wp\/v2\/posts\/229"}],"collection":[{"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=229"}],"version-history":[{"count":1,"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=\/wp\/v2\/posts\/229\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=\/wp\/v2\/posts\/229\/revisions\/230"}],"wp:attachment":[{"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.timwyatt.ca\/test\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}