随笔分类 -  读《Python自动化编程实战》笔记

摘要:1. 读取csv文件: >>> import csv>>> with open('top_films.csv') as file:... data = csv.reader(file)... for row in data:... print(row)... ['Rank', 'Admissions 阅读全文
posted @ 2022-04-13 10:28 轻舞飞洋 阅读(211) 评论(0) 推荐(0) 编辑
摘要:1. 用默认方式打开: (.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ echo $LANGzh_CN.UTF-8(.venv) (base) metal@metal-Lenovo-Produ 阅读全文
posted @ 2022-04-13 08:49 轻舞飞洋 阅读(184) 评论(0) 推荐(0) 编辑
摘要:一、读取文件,并通过筛选后打印出来: (.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ cat zen_of_python.txt The Zen of Python, by Tim Peter 阅读全文
posted @ 2022-04-11 12:41 轻舞飞洋 阅读(34) 评论(0) 推荐(0) 编辑
摘要:一、主要方法为os.walk(): (.venv) (base) metal@metal-Lenovo-Product:~/project/PAutomationCookbook/ch04$ mkdir dir(.venv) (base) metal@metal-Lenovo-Product:~/p 阅读全文
posted @ 2022-04-11 11:50 轻舞飞洋 阅读(25) 评论(0) 推荐(0) 编辑
摘要:这1章,花1个小时左右就看完了,与第1单有不少联系。但真正操作验证,还挺耗时的。所有代码都手工敲了一遍,加深了不少印象。 阅读全文
posted @ 2022-04-11 11:27 轻舞飞洋 阅读(2) 评论(0) 推荐(0) 编辑
摘要:一、发送邮件通知代码: import argparse import configparser import smtplib from email.message import EmailMessage def main(to_email, server, port, from_email, pas 阅读全文
posted @ 2022-04-11 00:01 轻舞飞洋 阅读(26) 评论(0) 推荐(0) 编辑
摘要:一、未对错误处理时: import argparse import sys def main(number, other_number, output): result = number / other_number print(f'The result is {result}', file=out 阅读全文
posted @ 2022-04-10 23:20 轻舞飞洋 阅读(10) 评论(0) 推荐(0) 编辑
摘要:一、编程定时任务的脚本: import argparse import sys from datetime import datetime import configparser def main(number, other_number, output): result = number * ot 阅读全文
posted @ 2022-04-10 22:45 轻舞飞洋 阅读(50) 评论(0) 推荐(0) 编辑
摘要:一、普通接收参数 import argparse def main(number, other_number): result = number * other_number print(f'The result is {result}') if __name__ == '__main__': pa 阅读全文
posted @ 2022-04-10 22:11 轻舞飞洋 阅读(19) 评论(0) 推荐(0) 编辑
摘要:使用argparse添加命令行参数,代码如下: import argparse def main(number): print('#' * number) if __name__ == '__main__': parser = argparse.ArgumentParser() #parser.ad 阅读全文
posted @ 2022-04-09 22:16 轻舞飞洋 阅读(38) 评论(0) 推荐(0) 编辑
摘要:示例如下: >>> import re >>> match = re.search(r'the phone number is ([\d-]+)', '37: the phone number is 1234-567-890') #\d: 标记任何数字(0-9)。 >>> match.group() 阅读全文
posted @ 2022-04-09 21:34 轻舞飞洋 阅读(23) 评论(0) 推荐(0) 编辑
摘要:一、示例如下: >>> import re #导入正则表达式模块 >>> re.search(r'LOG', 'SOME LOGS') <re.Match object; span=(5, 8), match='LOG'> #在字符串中查找'LOG' >>> re.search(r'^LOG', ' 阅读全文
posted @ 2022-04-09 11:41 轻舞飞洋 阅读(35) 评论(0) 推荐(0) 编辑
摘要:>>> from parse import parse >>> LOG = '[2018-05-05T12:58:00.714611] - SALE - PRODUCT: 1345 - PRICE: $09.99' >>> FORMAT = '[{date}] - SALE - PRODUCT: { 阅读全文
posted @ 2022-04-08 21:07 轻舞飞洋 阅读(56) 评论(0) 推荐(0) 编辑
摘要:一、例子: >>> import delorean >>> from decimal import Decimal >>> >>> log = '[2018-05-05T11:07:12.267897] - SALE - PRODUCT: 1345 - PRICE: $09.99' >>> devi 阅读全文
posted @ 2022-04-08 20:39 轻舞飞洋 阅读(50) 评论(0) 推荐(0) 编辑
摘要:简介一下"if ... else ... for" 的用法: >>> word = 'AFTER' >>> print('x' if w.isdigit() else w for w in word) <generator object <genexpr> at 0x105439750> 打印的结果 阅读全文
posted @ 2022-04-08 17:50 轻舞飞洋 阅读(28) 评论(0) 推荐(0) 编辑
摘要:使用template.format(*parameters)函数创建带格式的字符串。 代码如下: #!/usr/bin/env python data = [ (1000, 10), (2000, 17), (2500, 170), (2500, -170), ] # Print the heade 阅读全文
posted @ 2022-04-08 12:33 轻舞飞洋 阅读(31) 评论(0) 推荐(0) 编辑
摘要:通过配置文件来安装所需要的第三方软件包 一、编辑requirements.txt文件,如下: (.venv) huangsiyangdeiMac:projects huangsiyang$ cat requirements.txt delorean==1.0.0 requests==2.18.4 二 阅读全文
posted @ 2022-04-08 11:52 轻舞飞洋 阅读(118) 评论(0) 推荐(0) 编辑
摘要:以下操作在mac系统上执行,Linux系统类似,而Windows的路径有点区别,主要是activate文件是在Scripts下,同时需要先执行set-executionpolicy remotesigned之后,再执行./.venv/Scripts/Activate.ps1 使用命令python - 阅读全文
posted @ 2022-04-08 11:41 轻舞飞洋 阅读(58) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示