• 【Python】一些零散的练习


    #练习:subprocess模块来产生子进程
    import subprocess
    
    obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
    
    stderr=subprocess.PIPE)
    obj.stdin.write("print 1 
    ")
    obj.stdin.write("print 2 
    ")
    obj.stdin.write("print 3 
    ")
    obj.stdin.write("print uio 
    ")
    obj.stdin.close()
    
    cmd_out = obj.stdout.read()
    obj.stdout.close()
    cmd_error = obj.stderr.read()
    obj.stderr.close()
    
    print cmd_out
    print cmd_error
    
    #练习:将错误码为200的access log找出来写到另一个文件中,写入的内容包括IP地址,时间,访问地址
    import re
    def access_log():
        try:
            with open("e:\test4\decode.txt","r") as f1:
                for line in f1.readlines():
                    result=re.search(r"s(200)s",line)
                    if result:
                        time=re.search(r"([.*])",line).group(1)
                        resource=re.search(r"GET.*HTTP/1.1",line).group()
                        with open("e:\test4\decode_result.txt","a+") as f2:
                            f2.write(line[:15]+" "+time+" "+resource+"
    ")
        except Exception,e:
            print "error occurred"
    
    if __name__=="__main__":
        access_log()
    
    #练习:有一串字符串,比如string=”fjeuifhueifhyaf246438%&(+_(feufefuieadgd“,计算出每个字母出现了多少次,写出算法即可
    1、创建一个字典dicts,key存字母,value存字母出现的次数,for循环这个string,判断if "某个字母".isalpha()并且dicts.has_key("某个字母"),则dicts["某个字母"]+=1,最后打印出dicts
    2、for 循环string,用string.count("某个字母")计算某个字母出现的个数
    3、正则:遍历string,用len(re.findall(r”某个字符”,string))求出这个字符对应的长度
  • 相关阅读:
    编译安装php5 解决编译安装的php加载不了gd
    apache-php
    使用ab对网站进行压力测试
    正则表达式实例 -- 匹配Windows消息宏
    SDK 操作 list-view control 实例 -- 遍历进程
    malloc/free、new/delete 区别
    Python实例 -- 爬虫
    multimap实例 -- 添加、遍历数据
    CListCtrl 扩展风格设置方法---SetExtendedStyle和ModifyStyleEx
    创建指定大小的空文件
  • 原文地址:https://www.cnblogs.com/jingsheng99/p/8812677.html
Copyright © 2020-2023  润新知