• Python:日常应用汇总


    判断路径中是否包含中文

    import re
    def IsContainChinese(path:str) -> bool :
        cnPatter=re.compile(u'[u4e00-u9fa5]+')
        match=cnPatter.search(path)
        flag=False
        if match:
            flag=True
        else:
            flag = False
        return flag
    

    将文件保存为csv格式

    import csv

    def WriteResultToCSV(**kwags):
        v = [ v for v in kwags.values()]
        # v=lambda v:[ v for v in kwags.values()]
        # print(v)
        for item in v:
            try:
                header=["文件名","高度","宽度"]
                # 如果不加newline参数,则保存的csv文件会出现隔行为空的情况
                with open(os.getcwd()+"\result.csv",'w+',newline="") as fw:
                    csvWriter=csv.writer(fw)
                    csvWriter.writerow(header)
                    # print(item.items())
                    for k,v in item.items():
                        print(f"{k} {v}")
                        csvWriter.writerow([k,str(v[0]),str(v[1])])
            except Exception as e:
                pass
    

    获取图片分辨率

    • 方法一:通过opencv该方法不支持路径或文件名含有中文

    python opencv2安装: pip install opencv-python

    import cv2
    def GetResolution(path,imgList):
        temDict={}
        for item in imgList:
            #  opencv 不支持中文路径
            img=cv2.imread(path+"\"+item)
            # cv2.namedWindow("Image")
            # cv2.imshow("Image",img)
            # cv2.waitKey(1)
            # cv2.destroyAllWindows()
            imgResolution=img.shape
            temDict[item]=imgResolution
        return temDict
    
    • 方法二:通过opencv
               import cv2
               import numpy as np
               # 使用该方法时,路径中可含有中文,其中tmp为完整的图片路径
                img=cv2.imdecode(np.fromfile(tmp,dtype=np.uint8),cv2.IMREAD_COLOR)
              # 获取图片高度和宽度
              imgHeight,imgWidth=img.shape[:2]
    
    • 方法三:通过Pillow

    pip install Pillow

    from PIL import Image
    def GetImgSize(path):
        """
        path:传入完整路径 
        """
    
        img=Image.open(path)
        imgWidth,imgHeight=img.size
    
    

    获取文件夹内特定的文件

    import os
    
    def GetImgList(path):
        imgList=[ img for img in os.listdir(path)
                  if os.path.isfile(os.path.join(path,img)) and (img.endswith(".jpg") or img.endswith(".jpge") or img.endswith(".png"))
        ]
        return imgList
    
    

    将图片转换为Base64编码

    import base64
    
    def ConverImgToBase64(path,imgList):
        resultList={}
        for img in imgList:
            try:
                with  open (path+"\"+img,'rb') as fr:
                    data=base64.b64encode(fr.read())
                    tempResult=data.decode()
                    resultList[img]=tempResult
            except Exception as e:
                resultList["Exception"]=e
        return resultList
    

    生成MD5码

    # 生成MD5码
    def GenerateMD5Code(sku,secretKey='e10adc3949ba59abbe56e057f20f883e'):
        md5=hashlib.md5()
        encodeStr=secretKey+sku
        md5.update(encodeStr.encode('utf8'))
        
        return md5.hexdigest()  
    

    判断文件或文件夹是否存在

    import os
    def IsExistFile(path):
        try:
            if (os.path.exists(path)):
                os.remove(path)
        except Exception as identifier:
            pass
    

    比较文件差异

    
    import os
    # 描述信息:一个文件夹内一张图片对应一个xml或者只有图片或只有xml
    
    def ListFile(path):
        imgList=[]
        xmlList=[]
        extendIsXmlCount=0
        extendIsImgCount=0
        for file in os.listdir(path):
            fileList=file.split(".")
            try:
                if fileList[-1] == "xml":
                    extendIsXmlCount+=1
                    xmlList.append(fileList[0])
                elif fileList[-1] == "jpg" or fileList[-1] == "jpeg" or fileList[-1] == "png":
                    extendIsImgCount+=1
                    imgList.append(fileList[0])
            except Exception as e:
                print("error")
        differenceResult=set(imgList+xmlList)
        return imgList,xmlList,extendIsImgCount,extendIsXmlCount,differenceResult
    
    def CompareCount(xmlCount,imgCount):
        '''
        -1: xml >  img
        0 : xml == img
        1 : xml <  img
        '''
        # compareResult=-9999
        differenceCount=-9999
        if (xmlCount > imgCount):
            # print(f"xml Count {xmlCount} is more than img Count {imgCount} ,difference is {xmlCount-imgCount}")
            compareResult=f"xml Count {xmlCount} is more than img Count {imgCount} ,difference is {xmlCount-imgCount}"
            differenceCount=xmlCount-imgCount
        elif(xmlCount < imgCount):
            # print(f"xml Count {xmlCount} is less than img Count {imgCount} ,difference is {imgCount-xmlCount}")
            compareResult=f"xml Count {xmlCount} is less than img Count {imgCount} ,difference is {imgCount-xmlCount}" 
            differenceCount=imgCount-xmlCount
        elif (xmlCount == imgCount):
            # print(f"xml Count {xmlCount} is equal img Count {imgCount} ,difference is {imgCount-xmlCount}")
            compareResult=f"xml Count {xmlCount} is equal img Count {imgCount} ,difference is {imgCount-xmlCount}"
            differenceCount=imgCount-xmlCount
        return compareResult,differenceCount
    
    def RemoveDuplicateItem(ListA,ListB):
        tempSetA=set(ListA)
        tempSetB=set(ListB)
        if len(tempSetA) >= len(tempSetB):
           result=tempSetA-tempSetB
        else:
           result=tempSetB-tempSetA
        return result
    
    

    读取pkl文件

    import pickle
    def ReadFile(path):
        result=""
        try:
           with open (path,'rb') as fr:
               result=pickle.load(fr)
        except Exception as e:
            result=e
        return result
    

    存取为pkl文件

    import pickle
    def WriteStrToLogFile(path,dataStr):
        for i in range(2,5):
            PickleToFile(path+"\"+"error"+str(i)+".pkl",dataStr)
    
    def PickleToFile(path,fileName):
        with open(path,'wb') as fw:
            pickle.dump(dataStr,fw)
    

    将时间转换为Unix时间戳

    import time
    import datetime
    def ChangDateTimeToUnix(inputDateTime):
        '''
        将标准时间转换为Unix时间戳
        time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组
        time.strptime(string[, format])
        '''
        timeArray = time.strptime(inputDateTime, "%Y-%m-%d %H:%M:%S")
        timeStamp = int(time.mktime(timeArray))
        return timeStamp
    

    本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
    MyQRCode.jpg

  • 相关阅读:
    访问H2数据库的SpringBoot工程
    Spring Annotations
    衡量技术/工具好坏的标准
    Maven Waring : GroupId is duplicate of parent groupId 和 Version is duplicate of parent version
    MyEclipse中,使用Maven新建web项目后,提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    Java Web报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    maven项目,代码正确的情况下,整个项目中的类报cannot be resolved to a type 错误
    day46_Webservice学习笔记_02
    CentOS6 PXE+Kickstart无人值守安装
    快速诊断Linux性能
  • 原文地址:https://www.cnblogs.com/surpassme/p/11613019.html
Copyright © 2020-2023  润新知