pythonchanllenge-some learing notes

level 1(ocr):

Python maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。

注:两个字符串的长度必须相同,为一一对应的关系。

  • intab -- 字符串中要替代的字符组成的字符串。

outtab -- 相应的映射字符的字符串。

from string import maketrans  # 引用 maketrans 函数。

intab = "abcdefghijklmnopqrstuvwxyz"
outtab = "cdefghijklmnopqrstuvwxyzab"
trantab = maketrans(intab, outtab)

str1 = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
print str1.translate(trantab); 

level 2(equality):

获取pagesources的方法(后面可能经常会用,这个很变态啊因为后面很多关没说要用到pagesources( ╯□╰ ))

    def pro2(self):
        def get_web_url(url):
            import urllib
            web_content=urllib.urlopen(url)
            return "".join(web_content)

        content=get_web_url("http://www.pythonchallenge.com/pc/def/ocr.html")
        start="<!--"
        end="-->"
        # print content.index(start,789) #结果为838
        # print content.index(end,834) #结果为99608
        str="abcdefghijklmnopqrstuvwxyz"
        str_translate=""
        for r in content[838:99608]:
            if r in str:
                str_translate+=r
        print str_translate

level 3(linkedlist):

   运用到正则表达式re模块

    def pro3(self):
        import re

        content=self.get_web_url("http://www.pythonchallenge.com/pc/def/equality.html")
        letters=re.findall(r"[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]",content)
        answer=""
        for r in letters:
            answer+=r[4]
        print answer

level 4(peak.html):

    def pro4(self):
        import re

        nothing=63579
        i=1
        while i<=400:
            url="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing={}".format(nothing)
            web_content=self.get_web_url(url)
            nothing=re.findall(r"[0-9]{1,}",web_content)[0]
            print nothing,web_content
            if nothing==16044:
                nothing=16044/2
            if nothing==82682:
                nothing =63579
            i=i+1

 level 5(channel):

    def pro5(self):
        import pickle
        content=self.get_web_url("http://www.pythonchallenge.com/pc/def/banner.p")
        with open ("C:/Users/cuisy/Desktop/banner.txt","w") as f:
            f.write(content)
        with open("C:/Users/cuisy/Desktop/banner.txt", "r") as f:
            a=pickle.load(f)
        # print a
        for r in a:
            list=[]
            for i in r:
                list.append(i[0]*i[1])
            print "".join(list)

  level 6(hockey):

    def pro6(self):
        import os
        import wget
        import zipfile
        import re

        filepath=os.getcwd()
        wget.download("http://www.pythonchallenge.com/pc/def/channel.zip",out=filepath)
        # 下载文件并解压文件
        zipFile=zipfile.ZipFile("{}/channel.zip".format(filepath),"r")
        for file in zipFile.namelist():
            zipFile.extract(file,filepath)

        #获取文件中信息
        number=90052
        str=""
        while True:
            with open("{}/{}.txt".format(filepath,number),"r") as f:
                content = f.read()
                numberlist = re.findall(r"[0-9]{1,}", content)
                print content
                ZipInfo = zipFile.getinfo("{}.txt".format(number))
                str+=ZipInfo.comment
                if numberlist==[] :
                    break
                else:
                    number=numberlist[0]

        zipFile.close()
        print str

 level 6总结:

对压缩文件的操作需要用到zipfile中的zipFile类:

zipFile=zipfile.zipFile(filepath,“r”)#找到filepath路径下的zip文件

获取zip文件文件名称:zipFile.namelist()

解压zip中的某个文件至某个路径:zipFile.exetract(doc,filepath2) 解压zip文件下的doc文件到filepath2路径下

获取zip文件中的某个文件信息,需要用到getinfo()方法:

ZipInfo=zipFile.getinfo(doc) #获取doc文档信息

文件有如下信息,这里用到zipInfo.comment()(获取doc文件的文档说明):(列出来待需要时候用)

  • ZipInfo.filename: 获取文件名称。
  • ZipInfo.date_time: 获取文件最后修改时间。返回一个包含6个元素的元组:(年, 月, 日, 时, 分, 秒)
  • ZipInfo.compress_type: 压缩类型。
  • ZipInfo.comment: 文档说明。
  • ZipInfo.extr: 扩展项数据。
  • ZipInfo.create_system: 获取创建该zip文档的系统。
  • ZipInfo.create_version: 获取 创建zip文档的PKZIP版本。
  • ZipInfo.extract_version: 获取 解压zip文档所需的PKZIP版本。
  • ZipInfo.reserved: 预留字段,当前实现总是返回0。
  • ZipInfo.flag_bits: zip标志位。
  • ZipInfo.volume: 文件头的卷标。
  • ZipInfo.internal_attr: 内部属性。
  • ZipInfo.external_attr: 外部属性。
  • ZipInfo.header_offset: 文件头偏移位。
  • ZipInfo.CRC: 未压缩文件的CRC-32。
  • ZipInfo.compress_size: 获取压缩后的大小。
  • ZipInfo.file_size: 获取未压缩的文件大小。

 

posted @ 2017-11-27 15:26  cuisygan  阅读(369)  评论(0编辑  收藏  举报