python import csv file code example
Example 1: python read csv
import pandas as pd
data = pd.read_csv('data.csv')
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: command to read file in python using pandas
import panda as pd
file_csv = pd.read_csv("file path")
file_excel = pd.read_excel("file path")
file_json = pd.read_json("file path")
file_html = pd.read_html("file path")
file_localClipboard = pd.read_clipboard("file path")
file_MSExcel = pd.read_excel("file path")
file_HDF5 = pd.read_hdf("file path")
file_Feather = pd.read_feather("file path")
file_msgpack = pd.read_msgpack("file path")
file_stata = pd.read_stata("file path")
file_SAS = pd.read_sas("file path")
file_paythonPickle = pd.read_pickle("file path")
file_SQL = pd.read_sql("file path")
file_google_big_query = pd.read_gbq("file path")
Example 4: how to open csv file in python
import csv
with open('example.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
Example 5: how to read a csv file in python
import pandas as pd
df=pd.read_csv('the_file.csv')
Example 6: 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)