• python实用小方法


    一、创建随机字母

    import random
    
    # 创建随机字母
    def make_code(n):
        res = ''
        for i in range(n):
            num = str(random.randint(1, 9))  # 随机选取1到9的一个整数
            letter = chr(random.randint(65, 90))  # 随机选大写英文的一个字母
            group = random.choice([num, letter])  # 随机选取整数还是大写字母
            res += group  # 循环次数加到空字符串中
        return res
    print(make_code(4))

    二、glob正则匹配出需要的文件

    # 正则匹配出需要的文件
    import glob
    
    path = r"D:PycharmProjectsmapleface"
    for name in glob.glob('{}*[0-9].*'.format(path)):
        print(name)
    # 结果
    # D:PycharmProjectsmaplefaceMyface1.jpg
    # D:PycharmProjectsmaplefaceMyface2.jpg
    # D:PycharmProjectsmaplefaceMyface3.jpg
    # D:PycharmProjectsmaplefaceMyface4.jpg
    # D:PycharmProjectsmaplefaceMyface5.jpg

    # 匹配多种后缀名
    res=glob.glob('{}*.*[xlsx|XLS]'.format(r"********"))
    print(res)
     

    三、判断元素是否为数字

    import numpy as np
    # 判断元素是否为数字
    def is_number(s):
        try:
            if np.isnan(s) or s == False or s == True:
                return False
        except Exception:
            pass
        try:
            # 判断是否为浮点数
            float(s)
            return True
        except Exception:
            pass
        try:
            import unicodedata  # 处理ASCii码的包
            # 把一个表示数字的字符串转换为浮点数返回的函数
            unicodedata.numeric(s)
            return True
        except (TypeError, ValueError):
            pass
        return False
    
    
    numbers = [12, "43", "地方", None, np.nan, 88.99, False, True]
    print([is_number(i) for i in numbers])
    # 结果
    # [True, True, False, False, False, True, False, False]

    五、格式化

    # 格式化10进制
    print(format(10,"b"))
    # 格式化8进制
    print(format(10,"o"))
    # 格式化16进制
    print(format(10,"x"))

    六、精确处理数字

    # 精确处理数字
    a=1.1
    b=3.2
    print(a+b)
    
    from decimal import Decimal
    a=Decimal('1.1')
    b=Decimal('3.2')
    print(a+b)

    七、正则匹配

    # 正则匹配
    import re
    # 邮箱
    res1='email1:378533872@qq.com email2:333312312@163.com eamil3:alexsb123@gmail.com'
    res=re.findall('d+@w+.w+',res1)
    print(res)
    # 数字
    res2="1-12*(60+(-40.35/5)-(-4*3))"
    res=re.findall('(d+.d+|d+)',res2)
    print(res)

    八、flask-web

    from aiohttp import web
    async def index(request):
        return web.Response(text='Hello World')
    
    async def api(request):
        data = await request.json()
        question = data['question']
        return web.json_response({"state":question})
    
    app = web.Application()
    app.add_routes([web.get('/', index),
                    web.post('/api', api)])
    
    if __name__ == '__main__':
        web.run_app(app, host='127.0.0.1', port=5000)

    九、unicode转换

    s="u4E1Cu65B9u8BC1u5238"
    print(s.encode('unicode_escape').decode('unicode_escape'))
    #东方证券

    十、repr()的应用

    d=datetime.datetime(2020,1,2,0,0)
    print(d)
    #2020-01-02 00:00:00
    print(repr(d)) #返回该对象名字的str
    #datetime.datetime(2020, 1, 2, 0, 0)

    十一、节假日判断

    #安装pip install chinesecalendar
    
    from chinese_calendar import is_holiday
    today = datetime.datetime.now()
    res=is_holiday(today)
    print(res)

    十二、判断中文

    def is_Chinese(word):
        for ch in word:
            if 'u4e00' <= ch <= 'u9fff':
                return True
        return False
    
    
    def is_number(word):
        res = re.search("^[0-9]*$", word)
        if res:
            return True
        return False

    十三、判断文件的编码格式

    import chardet
    
    with open(r"*****","rb") as f:
        print (chardet.detect(f.read()))

    十四、pip加速

    pip install scrapy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

    十五、生成随机ChromeAgent

    import random
    
    first_num = random.randint(55, 62)
    third_num = random.randint(0, 3200)
    fourth_num = random.randint(0, 140)
    
    class FakeChromeAgent:
        os_type = [
                    '(Windows NT 6.1; WOW64)', '(Windows NT 10.0; WOW64)', '(X11; Linux x86_64)',
                    '(Macintosh; Intel Mac OS X 10_12_6)'
                   ]
    
        chrome_version = 'Chrome/{}.0.{}.{}'.format(first_num, third_num, fourth_num)
    
        @classmethod
        def get_agent(cls):
            return ' '.join(['Mozilla/5.0', random.choice(cls.os_type), 'AppleWebKit/537.36',
                             '(KHTML, like Gecko)', cls.chrome_version, 'Safari/537.36']
                            )
    
    print(FakeChromeAgent.get_agent())

    十六、获取本地mac地址

    import uuid
    
    # 获取mac地址
    addr_num = hex(uuid.getnode())[2:]
    mac = "-".join(addr_num[i: i + 2] for i in range(0, len(addr_num), 2))
    print(mac)

    十七、流下载大文件

    import requests
    
    url = "http://wx4.sinaimg.cn/large/d030806aly1fq1vn8j0ajj21ho28bduy.jpg"
    rsp = requests.get(url, stream=True)
    with open('1.jpg', 'wb') as f:
        for i in rsp.iter_content(chunk_size=1024):  # 边下载边存硬盘, chunk_size 可以自由调整为可以更好地适合您的用例的数字
            f.write(i)

    十八、redis有序集合实现权重取值

    import redis
    
    db_conn = redis.ConnectionPool(host="*.*.*.*", port=****, password="*****",db=0)
    redis_clint = redis.Redis(connection_pool=db_conn, max_connections=10)
    
    score=2 #分数值,数值越大,排序的时候越靠前
    # 添加值
    redis_clint.zadd("key_name", {"value001":score or 1})
    
    # 大到小排序取值
    res=redis_clint.zrevrange("key_name", 0, -1, withscores=False, score_cast_func=float)
    value=res[0].decode()
    
    # 判断数据是否存在,没有返回的None,有返回的是分数值
    res=redis_clint.zscore("key_name",value)
    print(res)
    
    # 处理数据后,删除值
    res=redis_clint.zrem("key_name",value)
    print(res)
  • 相关阅读:
    Innodb加载数据字典 && flush tables
    MySQL purge log简单吗
    MySQL ddl丢表
    数据库 一致性读&&当前读
    java数组
    customer.java
    java构造函数
    EXCEL 2007施工进度横道图制作步骤及实战练习
    如何利用office绘制施工进度计划横道图?
    计算器
  • 原文地址:https://www.cnblogs.com/angelyan/p/12116481.html
Copyright © 2020-2023  润新知