python csv file reader basics
#The following is the basic code for reading csv files.
#In this example, I have counted rows and columns of the csv file.
#100records.txt is the input file
code to copy
import csv
#open the file
with open('100records.txt','r') as csv_file: #Opens the file in read mode
csv_reader = csv.reader(csv_file,delimiter='\t') # Making use of reader method and using delimiter
row1=next(csv_reader) # read the first row
num_of_columns=len(row1) # counting columns
csv_file.seek(0) # reseting the file object pointer to first row
num_of_rows=len(list(csv_reader)) # counting number of rows
print(num_of_columns) # printing it
print(num_of_rows)

Comments
Post a Comment