美文网首页
python 提取和分割超大txt文件到csv文件

python 提取和分割超大txt文件到csv文件

作者: 牛奶大泡芙 | 来源:发表于2020-09-07 16:42 被阅读0次
    import csv
    import os
    
    NEW_CSV = ".\\generate"
    
    def trans_format(txt_path=""):
        if os.path.exists(txt_path):
    
            with open(txt_path, encoding='utf-8') as f:
                txt_row = "123"
                count = 0
                index = 0
                while txt_row:
                    txt_row = f.readline()
                    txt_row_list = txt_row.split('\t')
    
                    open_csv = open(NEW_CSV+str(count)+'.csv', 'a+',encoding='utf-8')
                    csv_writer = csv.writer(open_csv)
                    csv_writer.writerow(txt_row_list)
                    open_csv.close()
                    index += 1
                    if (index % 10000 == 0):
                        count = count+1
        else:
            print('can not transform')
        return
    
    if __name__ == "__main__":
        trans_format(".\\two")
    

    相关文章

      网友评论

          本文标题:python 提取和分割超大txt文件到csv文件

          本文链接:https://www.haomeiwen.com/subject/qmrlektx.html