当前位置 : 主页 > 编程语言 > python >

Python:日常应用汇总

来源:互联网 收集:自由互联 发布时间:2021-06-25
判断路径中是否包含中文 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 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

获取文件夹内特定的文件

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

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

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
网友评论