Python:日常应用汇总

Python:日常应用汇总,第1张

概述判断路径中是否包含中文 import redef IsContainChinese(path:str) -> bool : cnPatter=re.compile(u'[\u4e00-\u9fa5]+') match=cnPatter.search(path) flag=False if match: flag=True else: 判断路径中是否包含中文
import redef 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 cv2def 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
获取文件夹内特定的文件
import osdef 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 base64def 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
判断文件或文件夹是否存在
import osdef IsExistfile(path):    try:        if (os.path.exists(path)):            os.remove(path)    except Exception as IDentifIEr:        pass
比较文件差异
import os# 描述信息:一个文件夹内一张图片对应一个xml或者只有图片或只有xmldef 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,differenceResultdef 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,differenceCountdef 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 pickledef Readfile(path):    result=""    try:       with open (path,'rb') as fr:           result=pickle.load(fr)    except Exception as e:        result=e    return result
存取为pkl文件
import pickledef 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 timeimport datetimedef 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
总结

以上是内存溢出为你收集整理的Python:日常应用汇总全部内容,希望文章能够帮你解决Python:日常应用汇总所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1190877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存