当前位置: 移动技术网 > IT编程>脚本编程>Python > Python:日常应用汇总

Python:日常应用汇总

2019年09月30日  | 移动技术网IT编程  | 我要评论

瓦纳多,现爱韩国电影完整版,唯美签名

判断路径中是否包含中文

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网