Python reads and writes csv files,
Python comes with a csv module. For more details, refer to the official documentation.
I. Read operations
I don't know why, if the 'B' mode is not used when opening the file, there will be a line break. Therefore, to operate the csv file normally in windows, add the B mode. Delimiter to specify the delimiter between the fields of the reader.
Def readData (): with open('csvFile.csv ', 'rb') as fobj: csvFileReader = csv. reader (fobj, delimiter = '-'); header = next (csvFileReader); print header for row in csvFileReader: print row;
Ii. Write operations
Header = ["name", "gender", "Age"]; datas = [("Li Fei", "male", 15), ("Zhang Shan", "male ", 25)]; with open('csvFile.csv ', 'wb') as fobj: csvFileWriter = csv. writer (fobj, delimiter = '-'); csvFileWriter. writerow (header); csvFileWriter. writerows (datas );
These are simple csv file read and write operations. For more information, see the official documentation.