Python has a package that reads and writes a CSV file, directly import CSV
New Test.csv
1. Write
Import Csvwith Open ("Test.csv","W", encoding='UTF8') asCsvfile:writer=Csv.writer (CSVFile) writer.writerow (["Index","A_name","B_name"]) writer.writerows ([[0, ' A1 ', ' B1 '],[1, ' A2 ', ' B2 '],[2, ' A3 ', ' B3 '])
Using this notation directly will result in one more empty line behind each line of the file
How to Solve
When writing wirterow with Python3, use W mode when opening the file, then take newline= "
Import Csvwith Open ("Test.csv","W", encoding='UTF8', newline="') asCsvfile:writer=Csv.writer (CSVFile) writer.writerow (["Index","A_name","B_name"]) writer.writerows ([[0, ' A1 ', ' B1 '],[1, ' A2 ', ' B2 '],[2, ' A3 ', ' B3 '])
2. Read
Import Csvwith Open ("test.csv","R" as CSVFile: Reader=csv.reader (csvfile) for in Reader: Print (line)
Python reads CSV file