{"id":4129,"date":"2023-08-29T12:00:40","date_gmt":"2023-08-29T06:30:40","guid":{"rendered":"https:\/\/cbsepython.in\/?p=4129"},"modified":"2023-08-29T12:00:40","modified_gmt":"2023-08-29T06:30:40","slug":"csv-file-in-python-class-12-notes","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/csv-file-in-python-class-12-notes\/","title":{"rendered":"Working with CSV File in Python Class 12 Notes"},"content":{"rendered":"<h2><span style=\"color: #000000;\">CSV File in Python Class 12 Notes<\/span><\/h2>\n<h3><span style=\"color: #000000;\">What is CSV File<\/span><\/h3>\n<ul>\n<li><span style=\"color: #000000;\">A Comma Separated Values (CSV) file is a plain text file that contains the comma (,) separated data.<\/span><\/li>\n<li><span style=\"color: #000000;\">These files are often used for exchanging data between different applications.<\/span><\/li>\n<li><span style=\"color: #000000;\">CSV files are usually created by programs that handle huge amounts of data. They are used to export data from spreadsheets (ex:- excel file) and databases (Ex:- Oracle, MySQL). It can be used to import data into a spreadsheet or a database.<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">CSV File Structure<\/span><br \/>\n<span style=\"color: #000000;\">Name, DOB, City<\/span><br \/>\n<span style=\"color: #000000;\">Ram, 12-Jul-2001, Delhi<\/span><br \/>\n<span style=\"color: #000000;\">Mohan, 23-Jan-2005, Delhi<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">Python CSV Module<\/span><\/h3>\n<ul>\n<li><span style=\"color: #000000;\">CSV Module is available in Python Standard Library.<\/span><\/li>\n<li><span style=\"color: #000000;\">The CSV module contains classes that are used to read and write tabular form of <\/span><span style=\"color: #000000;\">data into CSV format.<\/span><\/li>\n<li><span style=\"color: #000000;\">To work with CSV Files, programmer have to import CSV Module.<\/span><br \/>\n<span style=\"color: #000000;\">import csv<\/span><\/li>\n<\/ul>\n<h3><span style=\"color: #000000;\">Methods of CSV Module :<\/span><\/h3>\n<ol>\n<li><span style=\"color: #000000;\">writer( )<\/span><\/li>\n<li><span style=\"color: #000000;\">reader( )<\/span><\/li>\n<\/ol>\n<p><span style=\"color: #000000;\">Both the methods return an Object of writer or reader class. Writer Object again have two <\/span><span style=\"color: #000000;\">methods \u2013 <\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\">writerow( ) <\/span><\/li>\n<li><span style=\"color: #000000;\">writerows( )<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">writer( ) Methods<\/span><\/h3>\n<p><span style=\"color: #000000;\">This function returns a writer object which is used for converting the data given by the <\/span><span style=\"color: #000000;\">user into delimited strings on the file object.<\/span><\/p>\n<h3><span style=\"color: #000000;\">writer( ) Object Methods \u2013<\/span><\/h3>\n<ul>\n<li><span style=\"color: #000000;\">w_obj . writerow( &lt;Sequence&gt; ) : Write a Single Line<\/span><\/li>\n<li><span style=\"color: #000000;\">w_obj . writerows ( &lt;Nested Sequence&gt; ) : Write Multiple Lines<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">writerow() Example:-<\/span><\/h3>\n<p><span style=\"color: #000000;\">import csv<\/span><br \/>\n<span style=\"color: #000000;\">row=[&#8216;Nikhil&#8217;, &#8216;CEO&#8217;, &#8216;2&#8217;, &#8216;9.0&#8217;]<\/span><br \/>\n<span style=\"color: #000000;\">f=open(&#8220;myfile.csv&#8221;, &#8216;w&#8217;)<\/span><br \/>\n<span style=\"color: #000000;\">w_obj = csv.writer(f)<\/span><br \/>\n<span style=\"color: #000000;\">w_obj.writerow(row)<\/span><br \/>\n<span style=\"color: #000000;\">f.close()<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">writerows() Example:<\/span><\/h3>\n<p><span style=\"color: #000000;\">import csv<\/span><br \/>\n<span style=\"color: #000000;\">rows = [[&#8216;Nikhil&#8217;,&#8217;CEO&#8217;,&#8217;2&#8242;,&#8217;9.0&#8242;],<\/span><br \/>\n<span style=\"color: #000000;\">[&#8216;Sanchit&#8217;,&#8217;CEO&#8217;,&#8217;2&#8242;,&#8217;9.1&#8242;]]<\/span><br \/>\n<span style=\"color: #000000;\">f=open(&#8220;myfile.csv&#8221;,&#8217;w&#8217;)<\/span><br \/>\n<span style=\"color: #000000;\">w_obj = csv.writer(f)<\/span><br \/>\n<span style=\"color: #000000;\">w_obj.writerows(rows)<\/span><br \/>\n<span style=\"color: #000000;\">f.close()<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">reader( ) Methods<\/span><\/h3>\n<p><span style=\"color: #000000;\">This function returns a reader object which will be used to iterate over lines of a given <\/span><span style=\"color: #000000;\">CSV file.<\/span><\/p>\n<p><span style=\"color: #000000;\">r_obj = csv.reader(csvfile_obj)<\/span><\/p>\n<p><span style=\"color: #000000;\">To access each row, we have to iterate over this Object.<\/span><br \/>\n<span style=\"color: #000000;\">for i in r_obj:<\/span><br \/>\n<span style=\"color: #000000;\">print(i)<\/span><\/p>\n<p><span style=\"color: #000000;\">import csv<\/span><br \/>\n<span style=\"color: #000000;\">f=open(&#8220;myfile.csv&#8221;,&#8217;r&#8217;)<\/span><br \/>\n<span style=\"color: #000000;\">r_obj = csv.reader(f)<\/span><br \/>\n<span style=\"color: #000000;\">for data in r_obj:<\/span><br \/>\n<span style=\"color: #000000;\">print(data)<\/span><br \/>\n<span style=\"color: #000000;\">f.close()<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSV File in Python Class 12 Notes What is CSV File A Comma Separated Values (CSV) file is a plain text file that contains the comma (,) separated data. These files are often used for exchanging data between different applications. CSV files are usually created by programs that handle huge amounts of data. They are [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,20],"tags":[],"class_list":["post-4129","post","type-post","status-publish","format-standard","hentry","category-cbse-sample-papers-class-12","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/4129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=4129"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/4129\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=4129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=4129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=4129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}