It is common to read and write CSV files at work. Record, to facilitate their own future inquiries and share with you:
Write CSV file method one:
Import CSV #导入CSV
With open ("D:\egg.csv", "WB") as CSVFile #新建一个叫egg. csv "file on D-disk.
A=csv.writer (csvfile) #以CSV的格式 write data to file CSVFile.
A.writerow (["Name", "Age", "tel"]) #写入的数据.
Print CSVFile #打印
Csvfile.close #关闭文件
Write CSV file Method two:
Same principle as method one.
import csv
csvfile= File ( "D:\egg2.csv", A=csv.writer ( csvfile)
A.writerow ([ "name", " tel "])
print csvfile
csvfile.close ()
Read csv file
Import CSV
# csvfile=file ("D:\egg2.csv", "RB")
# Reader=csv.reader (CSVFile)
# for-line in reader: #用FOR A walk through, you can read the contents of the file.
# Print Line
is not very simple, you can also, hands-on practice up.
How to read and write CSV format files in Python