Then in CSV it will be stored like below (Values containing comma will be enclosed in double quotes) Show Python Provides CSV module to work with csv file:Main Functions are:
How to read entire data from data.csv file?Let we try to read the following file ie “data.csv” data.csv CODING OUTPUTimport csv f = open(“data.csv”, ‘r’) row = csv.reader(f) for i in row : print(i) [‘Name’ , ‘Class’ , ‘Subject’] [‘Amit’ , ‘XII’ , ‘ CS’] [‘Sumit’ , ‘X’ , ‘IP’] Notice that each line in the CSV file is returned as a list of strings Python CSV File Line-wise explanation of the code
f = open(“data.csv”, ‘r’) row = csv.reader(f) print(row) Returns the memory address where csv.reader object is stored Python CSV File How to read specific column from data.csv? f = open(“data.csv”, ‘r’) row = csv.reader(f) for i in row : print(i[0] , i[2]) The above code is reading first and third column from “data.csv” file Name Subject Amit CS Sumit IP[‘Name’ , ‘Class’ , ‘Subject’] [‘Amit’ , ‘XII’ , ‘ CS’] [‘Sumit’ , ‘X’ , ‘IP’] Python CSV File If you want to skip the First Row (header of the table) CODING OUTPUT Actual Data of File import csvf = open(“data.csv”, ‘r’) row = csv.reader(f) next(row) for i in row : print(i[0] , i[2]) next () function will jump to the next row. import csv f = open(“data.csv”, ‘r’) row = csv.reader(f) next(row) next(row) for i in row : print(i[0] , i[2]) If we use the next() function two times then it will skip two lines from beginning [ ‘Amit’ , ‘CS’] [‘Sumit’ , ‘IP’ ] [‘Sumit’ , ‘IP’ ] [‘Name’ , ‘Class’ , ‘Subject’] [‘Amit’ , ‘XII’ , ‘ CS’] [‘Sumit’ , ‘X’ , ‘IP’] [‘Name’ , ‘Class’ , ‘Subject’] [‘Amit’ , ‘XII’ , ‘CS’] [‘Sumit’ , ‘X’ , ‘IP’] Python CSV File _______________________________________________________ If there is any other delimiter other than comma like in the following file f = open(“data.csv”, ‘r’) row = csv.reader(f, delimiter = ‘l’) for i in row : print(i) [‘id’ , ‘name’ , ’email’ , ‘salary’] [‘1’ , ‘Amit’ , ‘[email protected]’ , ‘25000’] [‘2’ , ‘Sumit’ , ‘[email protected]’ , ‘30000’] [‘3’ , ‘Ashu’ , ‘[email protected]’ , ‘40000’]
There are two main methods used in writing in csv file
Example using writerow() CODING DATA.CSV import csvf = open(“data.csv”, ‘w’) wr = csv.writer(f) wr.writerow([‘Name’ , ‘Class’]) wr.writerow([‘Amit’ , ‘XII’]) f.close( )Name , Class ‘Amit’ , ‘XII’Python CSV File Example using writerows() fields = [‘Name’ , ‘Class’ , ‘Subject’] rows = [[“Amit” , ‘XII’ , ‘CS’] , [‘Sumit’ , ‘X’ , ‘IP’] , [‘Ashu’ , ‘XI’ , ‘CS’]] f = open(“data.csv” , ‘w’ , newline = ‘ ‘) wr = csv.writer(f) wr.writerow(fields) wr.writerows(rows) f.close Name, Class, Subject Amit, XII, CS Sumit, X, IP Ashu, XI, CS Python CSV File Reading a CSV file with DictReader: This function(DictReader) is working similar to reader(). This function return lines as a dictionary instead of list. How to read CSV file in Python class 12?import csv. f = open(“data.csv”, 'r') row = csv.reader(f). next(row) for i in row : print(i[0] , i[2]). next () function will jump to the next row.. import csv. f = open(“data.csv”, 'r') row = csv.reader(f). next(row) next(row) for i in row : ... . If we use the next() function two times.. then it will skip two lines from beginning.. What is CSV function class 12?CSV file A CSV ( Comma Separated Value ) file is a text file that stores tabular data in simple text format separated by a separator. The default separator is comma (,) but any other single character can be used as a separator.
How to create CSV file in Python?How to Create CSV File in Python?. Open the CSV file in writing (w mode) with the help of open() function.. Create a CSV writer object by calling the writer() function of the csv module.. Write data to CSV file by calling either the writerow() or writerows() method of the CSV writer object.. Finally, close the CSV file.. What is CSV file in Python with example?A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data. Because it's a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters. The structure of a CSV file is given away by its name.
|