zoukankan      html  css  js  c++  java
  • 20171026python读取txt写入csv

    import csv
    
    def get_lines(filepath):
        with open(filepath) as file_object:
            lines=set(file_object.readlines())
            return lines
        
    def new_csv(lines):
        fileindex=0
        count=len(lines)
        print("总行数"+str(count))
        for index,line in enumerate(lines):
            index+=1
            #print(str(index)+'_'+line)
            oneline=line.strip()#逐行读取,剔除空白
            if (index-1) % 1000 +1 ==1:
                data=[]
                if len(oneline)==11:
                    data.append([oneline])
            elif index % 1000 ==0 or index==count:
                fileindex+=1
                if len(oneline)==11:
                    data.append([oneline])
                with open(str(fileindex)+'.csv','w') as csvfile:
                    csv_writer=csv.writer(csvfile,dialect='excel')
                    #csv_writer=csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
                    csv_writer.writerows(data)
            else:
                if len(oneline)==11:
                    data.append([oneline])
    if __name__ == "__main__":
        filepath="Text.txt"
        lines=get_lines(filepath)
        new_csv(lines)
    

      

  • 相关阅读:
    P3275 [SCOI2011]糖果 题解
    hdu 2962 题解
    hdu 2167 题解
    hdu 2476 题解
    hdu 5418 题解
    2019.10.16&17小结
    poj 3061 题解(尺取法|二分
    poj 1852&3684 题解
    NOIP2017[提高组] 宝藏 题解
    一类经典问题的解法
  • 原文地址:https://www.cnblogs.com/nextseven/p/7735664.html
Copyright © 2011-2022 走看看