1. Python has a dedicated CSV package that can be imported directly.
Import CSV;
2. Open method for direct use of ordinary files
Csv_reader=open ("E:/python/csv_data/log.csv", ' R ') Data=[]for line in Csv_reader: data.append (List (Line.strip () . Split (' | ')) For on data: print (line)
3. Return to iteration type using Csv.reader & writer
Import sys;reload (SYS) sys.setdefaultencoding ("Utf-8"); Csv_reader=csv.reader (file (' E:\\python\csv_data\log.csv '), ' RB '))) for row in Csv_reader: print (Row) writer=csv.writer (file (' E:\\python\csv_data\log_write.csv ', ' WB ') Writer.writerow ([' Name ', ' id ', ' comment ']) Lines=[range (3) for I in range (5)]for line in lines: Writer.writerow (line )
4. Use CSV. Dictreader & Dictwriter, returns the Dict dictionary type.
Reader1=csv. Dictreader (File (' e:\\python\csv_data\women_write.csv ', ' RB '))
Reference
Python: Read and write to a CSV file