Python常用代码备忘
使用清华pip源安装软件
1
pip3 install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple/
去除HTTP协议头,
http://baidu.com
->baidu.com
1
domain = re.sub('https?://', '', domain, flags=re.IGNORECASE)
把字符串内多个空格合并成一个空格,
' '
->' '
1
line = re.sub(' +', ' ', line)
超大文件读取, 超大文件读取使用fileinput,会逐行读取,而readlines会一次读完导致内存爆炸
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# def read_ip_history_file(filename):
# with open(filename, 'r', encoding='utf-8') as f:
# for line in f.readlines():
# line = line.strip()
# yield line
def read_ip_history_file(filename):
for line in fileinput.input([filename], openhook = fileinput.hook_encoded("utf-8")):
yield line
# 或者
def read_ip_history_file(filename):
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
yield line日期转换
1
2
3
4
5# 字符串格式化为日期对象
mail_date = '2019-09-29 04:55:46'
strdate = datetime.datetime.strptime(mail_date, '%Y-%m-%d %H:%M:%S')
# 日期对象格式化为字符串对象
strdate.strftime('%Y%m%d')子线程随父线程退出
1
2
3
4write_flag_task = Thread(target=saveFlag, args=(LOG_FILE_NAME, ))
# 子线程随父进程退出
write_flag_task.setDaemon(True)
write_flag_task.start()拼接脚本依赖文件的绝对路径。比如:脚本依赖一个数据库文件,未防止他人使用脚本时路径异常读不到数据库,需要拼接路径。
1
2
3
4DBPATH = '123.db'
script_path = sys.argv[0]
abs_script_dir, _ = os.path.split(os.path.abspath(script_path))
DBPATH = os.path.join(abs_script_dir, DBPATH)提取中文的正则表达式
1
2
3s = '123你好a'
re.findall('[\u4e00-\u9fa5]+', s)
输出:['你好']openpyxl 修改Excel的字体样式
1
2
3
4
5
6
7
8
9
10
11main_wb = Workbook()
main_sheet = main_wb.active
# 添加Excel首行标题
main_sheet.append(
'站点URL,站点IP,漏洞名称,漏洞风险值,风险等级,CVE编号,详细描述,解决办法,存在漏洞链接,漏洞验证参数'.split(','))
# 定义标题字体格式
t_font = Font(name='宋体', size=12, bold=True)
for row in main_sheet.rows:
for cell in row:
cell.font = t_font
breakopenpyxl删除行:
删除第三行之后的两行,行号从1开始算1
sheet.delete_rows(3, 2)
删除第一列之后的两列,列号从1开始算
1
wk_sheet.delete_cols(1,2)