{"id":305,"date":"2013-09-26T12:15:00","date_gmt":"2013-09-26T12:15:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-6\/"},"modified":"2013-09-26T12:15:00","modified_gmt":"2013-09-26T12:15:00","slug":"the-python-challenge-6","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-6\/","title":{"rendered":"The Python Challenge &#8211; 6"},"content":{"rendered":"<p>This was fun, using python to access zip files.  I based it mostly on #4 with a small twist at the end to print out the final answer.  The obvious answer didn&#8217;t work &#8211; I thought I was looking at challenge 7 but realised after banging my head against the wall for a while that I was still on number 6.  What was really annoying was that I had already noticed the correct word whilst fiddling with the code. <\/p>\n<pre>****************************************************************<br>****************************************************************<br>**                                                            **<br>**   OO    OO    XX      YYYY    GG    GG  EEEEEE NN      NN  **<br>**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE  NN    NN   **<br>**   OO    OO XXX  XXX YYY   YY  GG GG     EE       NN  NN    **<br>**   OOOOOOOO XX    XX YY        GGG       EEEEE     NNNN     **<br>**   OOOOOOOO XX    XX YY        GGG       EEEEE      NN      **<br>**   OO    OO XXX  XXX YYY   YY  GG GG     EE         NN      **<br>**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE     NN      **<br>**   OO    OO    XX      YYYY    GG    GG  EEEEEE     NN      **<br>**                                                            **<br>****************************************************************<br> **************************************************************<\/pre>\n<h3>2.7 Solution<\/h3>\n<pre>#zip?<br>import zipfile, re, sys<br>data = zipfile.ZipFile(\"channel.zip\")<br>#data is now some sort of object<br>#data.namelist() is all the files in the archive<br><br>count = 1<br>final = []<br><br>def getNextCode(lastCode):<br>    while True:<br>        try:<br>            myfile = data.open(lastCode + '.txt')<br>            myinfo = data.getinfo(lastCode + '.txt')<br>            final.append(myinfo.comment)<br>            code = re.findall('(?<=Next nothing is )\\w*',myfile.read())<br>            #print \"Code %i is %s.  \\nPage read was '%s.txt'\\n\" % (count, code[0], lastCode)<br>            #print \"Code %i\\n\" % (count)<br>            return code<br>        except (IndexError, NameError):<br>            print \"Error\" #: Page message was '%s'\\nPage was: '%s.txt'.\" % (page,lastCode)<br>            return False<br><br>nextCode = getNextCode('90052')<br><br>for count in range (1,len(data.namelist())- 2):<br>    nextCode = getNextCode(nextCode[0])<br><br><br>myfile = data.open(nextCode[0] + '.txt').read()<br><br>#Collect the comments.<br>for character in final:<br>    sys.stdout.write(character)<br>#look at the message and think about it...<\/pre>\n<h3>3.2.3 Solution\u00a0 <\/h3>\n<pre>#zip?<br>import zipfile, re, sys<br>data = zipfile.ZipFile(\"channel.zip\")<br>#data is now some sort of object<br>#data.namelist() is all the files in the archive<br><br>count = 1<br>final = []<br><br>def getNextCode(lastCode):<br>    while True:<br>        try:<br>            myfile = data.open(lastCode + '.txt')<br>            myinfo = data.getinfo(lastCode + '.txt')<br>            final.append(myinfo.comment)<br>            code = re.findall(r'(?<=Next nothing is )\\w*',myfile.read().decode(\"utf-8\"))<br>            return code<br>        except (IndexError, NameError):<br>            print (\"Error\")<br>            return False<br><br>nextCode = getNextCode('90052')<br><br>for count in range (1,len(data.namelist())- 2):<br>    nextCode = getNextCode(nextCode[0])<br><br><br>myfile = data.open(nextCode[0] + '.txt').read()<br><br>#Collect the comments.<br>for character in final:<br>    sys.stdout.write(character)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This was fun, using python to access zip files. I based it mostly on #4 with a small twist at the end to print out the final answer. The obvious answer didn&#8217;t work &#8211; I thought I was looking at challenge 7 but realised after banging my head against the wall for a while that &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-6\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The Python Challenge &#8211; 6<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[39],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","tag-python"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":0,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}