20171026python Read txt write CSV

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.