Python:日常应用汇总

匿名 (未验证) 提交于 2019-12-02 22:51:30
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

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

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
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
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
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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!