Import Csvdef 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 ("Total Rows" +str (count)) for index,line in EN Umerate (lines): Index+=1 #print (str (index) + ' _ ' +line) Oneline=line.strip () #逐行读取, excluding blank if (index- 1)% +1 ==1:data=[] If Len (oneline) ==11:data.append ([oneline]) elif IND Ex% ==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 Read txt write CSV