1# This action requires the 'csv' module
2import csv
3
4# The basic usage is to first define the rows of the csv file:
5row_list = [["SN", "Name", "Contribution"],
6 [1, "Linus Torvalds", "Linux Kernel"],
7 [2, "Tim Berners-Lee", "World Wide Web"],
8 [3, "Guido van Rossum", "Python Programming"]]
9
10# And then use the following to create the csv file:
11with open('protagonist.csv', 'w', newline='') as file:
12 writer = csv.writer(file)
13 writer.writerows(row_list)
14# This will create a csv file in the current directory
1import csv
2
3with open('names.csv', 'w') as csvfile:
4 fieldnames = ['first_name', 'last_name']
5 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
6
7 writer.writeheader()
8 writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
9 writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
10 writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
11
1import csv
2
3with open('example.csv') as csvfile:
4 readCSV = csv.reader(csvfile, delimiter=',')
1import pandas as pd # pip install pandas
2
3#read the CSV file
4data_file = =pd.read_csv('some_file.csv')
5print(data_file)
6
1>>> import csv
2>>> with open('names.csv', newline='') as csvfile:
3... reader = csv.DictReader(csvfile)
4... for row in reader:
5... print(row['first_name'], row['last_name'])
6...
7Eric Idle
8John Cleese
9
10>>> print(row)
11{'first_name': 'John', 'last_name': 'Cleese'}
12
1import csv
2csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
3with open('passwd', newline='') as f:
4 reader = csv.reader(f, 'unixpwd')
5