>>> import csv >>> with open('eggs.csv', newline='') as csvfile: ... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') ... for row in spamreader: ... print(', '.join(row)) Spam, Spam, Spam, Spam, Spam, Baked Beans Spam, Lovely Spam, Wonderful Spam
Here is what the above code is Doing:
1. The first line tells Python to open the eggs.csv file and create a file object called csvfile.
2. The second line creates a variable called spamreader that is assigned the value of the csv.reader() function.
3. The third line uses a for loop to loop through each row in the csvfile object.
4. The fourth line prints the row variable, which is a list that represents a row in the CSV file.
5. The fifth line closes the CSV file.