To read a CSV file:
Import CSV
#打开文件, with the open can not be deliberately closed file, Python3 does not support files () Open, only with open ()
With open ("Xxx.csv", "R", encoding= "Utf-8") as CSVFile:
#读取csv文件, the iteration type is returned
Read = csv. Reader (CSVFile)
For I in read:
Print (i)
Save As CSV file:
Import CSV
With open ("Xxx.csv", "W", Newline= "") as Datacsv:
#dialect为打开csv文件的方式, the default is excel,delimiter= "\ T" parameter refers to the delimiter at the time of writing
Csvwriter = csv. writer (Datacsv,dialect = ("Excel"))
#csv文件插入一行数据, put each item in the following list into a cell (you can insert multiple rows in a loop)
Csvwriter.writerow (["A", "B", "C", "D"])
Description:CSV module also has Dictreader and dictwriter can be used to read and write, return is the type of dictionary, but these two methods I have no use, interested can see for themselves.
Python3 using CSV module to read and write CSV files