python read in csv code example

Example 1: python read csv

# pip install pandas 
import pandas as pd

# Read the csv file
data = pd.read_csv('data.csv')

# Print it out if you want
print(data)

Example 2: csv python write

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})

Example 3: read csv python

import pandas as pd 
data = pd.read_csv("filename.csv") 
data.head()

Example 4: python read csv

>>> 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

Example 5: how to read a csv file in python

import pandas as pd
df=pd.read_csv('the_file.csv')

Example 6: read a csv file in pandas

you should be in the same dir as .py file 

df = pd.read_csv('your_file_name.csv')