what is csv file in python code example
Example 1: create csv file python
import csv
row_list = [["SN", "Name", "Contribution"],
[1, "Linus Torvalds", "Linux Kernel"],
[2, "Tim Berners-Lee", "World Wide Web"],
[3, "Guido van Rossum", "Python Programming"]]
with open('protagonist.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(row_list)
Example 2: how to open csv file in python
import pandas as pd
data_file = =pd.read_csv('some_file.csv')
print(data_file)
Example 3: how to open csv file in python
import csv
with open('example.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
Example 4: python csv
>>> import csv
>>> with open('names.csv', newline='') as csvfile:
... reader = csv.DictReader(csvfile)
... for row in reader:
... print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese
>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Example 5: import csv file in python
import pandas as pd
df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
print (df)