python学习笔记及常见错误处理 长期阶段性更新(一)

1.python解析URL中的参数

src = 'http://www.baidu.com/search?a=1'
parseUrl = parse.urlparse(src)
query_dict = parse.parse_qs(parseUrl.query)
a = query_dict.get('a')[0]

2.pymysql

// 初始化
class DB:
    def __init__(self):
        self.conn = pymysql.connect(
            host=configs.mysqlhost,
            port=configs.mysqlport,
            user=configs.mysqluser,
            password=str(configs.mysqlpassword),
            database=configs.mysqldatabase
        )
        self.cursor = self.conn.cursor()
// 基本使用
def test():
    db = DB()
    querySql = 'select id from tablename where ...'
    arr = []

    try:
        db.cursor.execute(querySql)
        id = db.cursor.fetchone()
        if id:
            ...   

    except Exception as e:
        print('test: ', e)
    finally:
        db.cursor.close()
        db.conn.close()

3.带索引遍历

for index, item in enumerate(arr):
    print(index, item)

4.python配置yaml

//pip install pyyaml

import yaml

config = None
try:
    f = open('config.yaml')
    data = yaml.load(f.read(), Loader=yaml.FullLoader)
    config = data['config']
    host = config['host']
    f.close()

except Exception as e:
    print('读取配置文件失败', e)
    
//config.yaml
config:
  host: www...com

5.将文本中特殊字符替换为空格

def tidy_text(text):
    txt = text
    for i in '!"#$%&()*+,-./:;<=>?@[\]^_{|}~,。、 :':
        txt = txt.replace(i, "")  # 将文本中特殊字符替换为空格
    return txt

6.文件常用操作

//拼接路径
os.path.join('path1', 'path2')
// 判断是否存在
os.path.exists(path)
// 创建文件夹
os.mkdir(path)
// 覆盖写入文件
with open(img_path, 'wb') as f:
    f.write(content)
    f.close()

7.带参数启动

parser = argparse.ArgumentParser(description='description')
parser.add_argument('--id', type=int, help='任务ID')
args = parser.parse_args()

if args.id is None:
    print('参数未补全')
    return

8.带session的网络请求

session = requests.session()
resp = session.post(url=startLoginUrl, data=params, headers=headers)

9.print转log

f = open('logs.txt', 'a')
sys.stdout = f
print('test')

10.python生成依赖清单

// 生成
pip freeze > requirements.txt
// 下载
pip install -r requriements.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值