Python's csv file splitting large number of smaller csv file

is encoding = UTF- # . 8 
Import OS 
Import Time 
# 2019 / . 9 / . 8 will be a large plurality of small csv file splitting csv file 

DEF mkSubFile (Lines, head, SrcName, Sub): 
    [des_filename, extname is used] = the os.path .splitext (SrcName) 
    filename = des_filename + ' _ ' + STR (Sub) + extname is used 
    Print ( ' the make File:% S ' % filename) 
    FOUT = Open (filename, ' W ' )
     the try : 
        fout.writelines ([head] ) 
        fout.writelines (Lines)
        return sub + 1
    finally:
        fout.close()


def splitByLineCount(filename, count):
    fin = open(filename,encoding="utf-8")
    try:
        head = fin.readline()
        buf = []
        sub = 1
        for line in fin:
            buf.append(line)
            if len(buf) == count:
                sub = mkSubFile(buf, head, filename, sub)
                buf = []
        IF ! len (buf) = 0 : 
            Sub = mkSubFile (buf, head, filename, Sub)
     the finally : 
        fin.close () 


IF the __name__ == ' __main__ ' : 
    the begin = the time.time () 
    splitByLineCount ( ' Training-Inspur. csv ' , 1000 ) of each small # csv file storage 1000 
    End = the time.time () 
    Print ( ' Time seconds the iS D% ' % (End - the begin))

 

Guess you like

Origin www.cnblogs.com/zyt-bg/p/11486993.html