• [Python]有用的小函数集合(不定期更新)


    #读csv
    def readCsvTo2DList(csvFullFileName,skipHeader,ignoreTail):
        listFile = open(csvFullFileName,'r')
        context  = listFile.read()
        contextList = context.split("
    ")
    
        for i in range(len(contextList)):
            contextList[i]=contextList[i].split(",")
        
        if skipHeader:
            contextList = contextList[1:len(contextList)]
        else:
            contextList = contextList[0:len(contextList)]
        
        if ignoreTail:
            contextList = contextList[0:len(contextList)-1]
        
        return contextList
    
    #获取指定文件夹下特定扩展名的文件列表
    def getSpecifiedFile(sourceDir,extension,sort):
        fileList = os.listdir(sourceDir)
        resultFileList = []
        for file in fileList:
            if os.path.splitext(file)[1]==extension:
                resultFileList.append(file)
        if sort:
            resultFileList.sort()
        for i in range(0,len(resultFileList)):
            resultFileList[i] = sourceDir+resultFileList[i]
        return resultFileList
    
    #写csv
    def write2DListToCsv(listItem,fileName):
        fileObject = open(fileName, 'w')
        for i in range(len(listItem)):
            for j in range(len(listItem[i])):
                fileObject.write(str(listItem[i][j]))
                if j < len(listItem[i])-1:
                    fileObject.write(",")
            if  i < len(listItem)-1: 
                fileObject.write("
    ")
        fileObject.close()
    
    #按指定字段对表排序
    def sort2DListBySpecifiedField(inputList,index,reverse):
        sortedList = sorted(inputList,key=lambda x:x[index],reverse=reverse)
        return sortedList
    
    #Python2 的中文处理,需要首行指定 # -*- coding: utf-8 -*-
    def chineseDecode(inputString):
        return inputString.decode("utf-8").encode("gbk")
    
  • 相关阅读:
    PHP Mail 简介
    二级叉的结算另一种方法
    PHP extract() 函数
    array_count_values函数
    对碰结算的方法
    array_walk() 函数
    函数引发二级叉的结算
    array_intersect() php筛选两个数组共有的元素
    php文件锁
    JAVA Math常用方法
  • 原文地址:https://www.cnblogs.com/wszhang/p/13273741.html
Copyright © 2020-2023  润新知