import csv
# open the file
with open('csvfile.csv' , 'r') as csvfile:
# create the object of csv.reader()
csv_file_reader = csv.reader(csvfile,delimiter=',')
for row in csv_file_reader:
print(row)
import csv
with open('data.csv', 'r') as f:
reader = csv.reader(f)
# loop through each row and print each value
for row in reader:
for e in row:
print(e)
with open('data.csv', 'r') as f:
# change the delimiter from the default comma to another delimiter
reader = csv.reader(f, delimiter="|")
for row in reader:
for e in row:
print(e)
nums = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
with open('numbers2.csv', 'w') as f:
writer = csv.writer(f)
# write arrays as rows to CSV file
for row in nums:
writer.writerow(row)
with open('numbers2.csv', 'w') as f:
writer = csv.writer(f, delimiter="+")
# write arrays as row to CSV file with + as the delimiter instead of commas
for row in nums:
writer.writerow(row)
with open(r'c:dlFrameRecentSessions.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(f' {row[0]} works in the {row[1]} department, and was born in {row[2]}.')
line_count += 1
print(f'Processed {line_count} lines.')
# importing Pandas library
import pandas as pd
pd.read_csv(filepath_or_buffer = "pokemon.csv")
# makes the passed rows header
pd.read_csv("pokemon.csv", header =[1, 2])
# make the passed column as index instead of 0, 1, 2, 3....
pd.read_csv("pokemon.csv", index_col ='Type')
# uses passed cols only for data frame
pd.read_csv("pokemon.csv", usecols =["Type"])
# returns pandas series if there is only one column
pd.read_csv("pokemon.csv", usecols =["Type"],
squeeze = True)
# skips the passed rows in new series
pd.read_csv("pokemon.csv",
skiprows = [1, 2, 3, 4])
import csv
def read_csv(path):
with open(path, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print("Row is: ",row)
read_csv(your_file_path)