最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

python入门学习练习题详解

XAMPP案例 admin 42浏览 0评论
AAAA000000013
本文记录了是学习python过程中做过的小练习题。
题1:按要求过滤日志logfile.log文件内容并写入不同分文件:
包含“ ERROR”的日志,并保存到文件“ error.log”中
包含“ WARNING”的日志,并保存到文件“ warning.log”中
不包含“ ERROR”和“ WARNING”的日志,保存到“ info.log”中

首先创建一个logfile.txt

WARNING 11 --- [freshExecutor-0] jdbc.resultset    : 10. ResultSet.wasNull() returned falseDEBUG [HeartbeatHandler.java:82] :  [DUBBO] Receive heartbeat response in thread New I/O client worker #1-1, dubbo version: 2.6.0INFO 11 --- [freshExecutor-0] jdbc.resultset    : 10. ResultSet.getString(8) returned nullWARNING 11 --- [freshExecutor-0] jdbc.resultset    : 10. ResultSet.next() returned falseERROR [HeartbeatHandler.java:82] :   Can not receive heartbeat response in thread New I/O clientDEBUG [HeartBeatTask.java:66] :  [DUBBO] Send heartbeat to remote channel

 

代码实现

写法一:打开文件的with open分层次写:

# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 16:42# User      : Administrator
with open("logfile.txt","r") as filelog: with open("error.log", encoding="utf-8", mode="a") as errorlog: with open("warning.log", encoding="utf-8", mode="a") as warninglog: with open("info.log", encoding="utf-8", mode="a") as infolog: for line in filelog.readlines(): if "ERROR" in line: errorlog.write(line) elif "WARNING" in line: warninglog.write(line) else: infolog.write(line)

写法二:还可以把所有with open写在一排:

# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 16:42# User      : Administrator
with open("logfile.txt","r") as filelog, open("error2.log", encoding="utf-8", mode="a") as errorlog, open("warning2.log", encoding="utf-8", mode="a") as warninglog, open("info2.log", encoding="utf-8", mode="a") as infolog: for line in filelog.readlines(): if "ERROR" in line: errorlog.write(line) elif "WARNING" in line: warninglog.write(line) else: infolog.write(line)

题2:请按照注释添加代码def is_to_show(aNumber):

    // 如果aNumber 是一个偶数,且大于10 ,则返回True 
return False
如果“ __main__” == __name__:
数字= [1,2,3,8,10,12,22,105,110,80,3,70]

// 遍历数字内的所有数字

// 数字中达成is_to_show 的条件的数字按照从小到大的顺序输出

写法一:先将数字分散再遍历数字,这样写的弊端就是不满足要求的数字也会加入排序,浪费资源:


# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 17:02# User      : Administratordef is_to_show(aNumber):if aNumber % 2 ==0 and aNumber > 10:        return True    return Falseif "__main__" == __name__:    numbers = [1,2,3,8,10,12,22,105,110,80,3,70]    numbers = sorted(numbers)    for aNumber in numbers:        if is_to_show(aNumber) == True:            print(aNumber)

写法二:将满足条件的数字输入到新的数组,然后将新的片断从小到大分开顺序后输出:

# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 17:22# User      : Administratordef is_to_show(aNumber):if aNumber % 2 ==0 and aNumber > 10:        return True    return Falseif "__main__" == __name__:    numbers = [1,2,3,8,10,12,22,105,110,80,3,70]    numbers_new = []    for aNumber in numbers:        if is_to_show(aNumber) == True:            numbers_new.append(aNumber)    numbers_new = sorted(numbers_new)    print(numbers_new)

未完待续......
本文记录了是学习python过程中做过的小练习题。
题1:按要求过滤日志logfile.log文件内容并写入不同分文件:
包含“ ERROR”的日志,并保存到文件“ error.log”中
包含“ WARNING”的日志,并保存到文件“ warning.log”中
不包含“ ERROR”和“ WARNING”的日志,保存到“ info.log”中

首先创建一个logfile.txt

WARNING 11 --- [freshExecutor-0] jdbc.resultset    : 10. ResultSet.wasNull() returned falseDEBUG [HeartbeatHandler.java:82] :  [DUBBO] Receive heartbeat response in thread New I/O client worker #1-1, dubbo version: 2.6.0INFO 11 --- [freshExecutor-0] jdbc.resultset    : 10. ResultSet.getString(8) returned nullWARNING 11 --- [freshExecutor-0] jdbc.resultset    : 10. ResultSet.next() returned falseERROR [HeartbeatHandler.java:82] :   Can not receive heartbeat response in thread New I/O clientDEBUG [HeartBeatTask.java:66] :  [DUBBO] Send heartbeat to remote channel

 

代码实现

写法一:打开文件的with open分层次写:

# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 16:42# User      : Administrator
with open("logfile.txt","r") as filelog: with open("error.log", encoding="utf-8", mode="a") as errorlog: with open("warning.log", encoding="utf-8", mode="a") as warninglog: with open("info.log", encoding="utf-8", mode="a") as infolog: for line in filelog.readlines(): if "ERROR" in line: errorlog.write(line) elif "WARNING" in line: warninglog.write(line) else: infolog.write(line)

写法二:还可以把所有with open写在一排:

# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 16:42# User      : Administrator
with open("logfile.txt","r") as filelog, open("error2.log", encoding="utf-8", mode="a") as errorlog, open("warning2.log", encoding="utf-8", mode="a") as warninglog, open("info2.log", encoding="utf-8", mode="a") as infolog: for line in filelog.readlines(): if "ERROR" in line: errorlog.write(line) elif "WARNING" in line: warninglog.write(line) else: infolog.write(line)

题2:请按照注释添加代码def is_to_show(aNumber):

    // 如果aNumber 是一个偶数,且大于10 ,则返回True 
return False
如果“ __main__” == __name__:
数字= [1,2,3,8,10,12,22,105,110,80,3,70]

// 遍历数字内的所有数字

// 数字中达成is_to_show 的条件的数字按照从小到大的顺序输出

写法一:先将数字分散再遍历数字,这样写的弊端就是不满足要求的数字也会加入排序,浪费资源:


# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 17:02# User      : Administratordef is_to_show(aNumber):if aNumber % 2 ==0 and aNumber > 10:        return True    return Falseif "__main__" == __name__:    numbers = [1,2,3,8,10,12,22,105,110,80,3,70]    numbers = sorted(numbers)    for aNumber in numbers:        if is_to_show(aNumber) == True:            print(aNumber)

写法二:将满足条件的数字输入到新的数组,然后将新的片断从小到大分开顺序后输出:

# encoding: utf-8# Author    : Crystal# Datetime  : 2020/4/25 17:22# User      : Administratordef is_to_show(aNumber):if aNumber % 2 ==0 and aNumber > 10:        return True    return Falseif "__main__" == __name__:    numbers = [1,2,3,8,10,12,22,105,110,80,3,70]    numbers_new = []    for aNumber in numbers:        if is_to_show(aNumber) == True:            numbers_new.append(aNumber)    numbers_new = sorted(numbers_new)    print(numbers_new)

未完待续......

转载请注明:XAMPP中文组官网 » python入门学习练习题详解