码迷,mamicode.com
首页 > 编程语言 > 详细

20171026python读取txt写入csv

时间:2017-10-26 11:37:13      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:ima   new   nim   log   open   __name__   div   rip   python   

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)

  

20171026python读取txt写入csv

标签:ima   new   nim   log   open   __name__   div   rip   python   

原文地址:http://www.cnblogs.com/nextseven/p/7735664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!