{"id":3079,"date":"2019-10-17T11:25:07","date_gmt":"2019-10-17T10:25:07","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3079"},"modified":"2020-05-29T09:04:24","modified_gmt":"2020-05-29T08:04:24","slug":"read-and-write-with-file-csv-in-python","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/","title":{"rendered":"Lire et \u00e9crire avec fichier CSV dans Python"},"content":{"rendered":"<p>[dropcap]I[\/dropcap]t is possible to&nbsp;<b>read and write CSV (comma separated values) files<\/b>&nbsp;using Python 2.4 Distribution. Like most languages, file operations can be done with Python.&nbsp;<b>Writing a CSV file&nbsp;<\/b>with Python can be done by importing the CSV module and creating a write object that will be used with the WriteRow Method.&nbsp;<b>Reading a CSV file<\/b>&nbsp;can be done in a similar way by creating a reader object and by using the print method to read the file. As file operations require advanced concepts, some knowledge of programming with Python is required to&nbsp;<b>read and write the CSV (comma separated values) files<\/b>.<\/p>\n<div id=\"toc\">\n<ul>\n<li><a href=\"#prerequisites\">Prerequisites<\/a><\/li>\n<li><a href=\"#writing-a-csv-file\">Writing a CSV file<\/a><\/li>\n<li><a href=\"#reading-a-csv-file\">Reading a CSV file<\/a><\/li>\n<\/ul>\n<\/div>\n<p>Python&nbsp;<b>Www.python.org<\/b>, version 2.4 supports the&nbsp;<i>de facto<\/i>&nbsp;<b>CSV<\/b>&nbsp;format (comma-separated values: ). The Reference Library is very helpful when learning to use Python. Here are some additional tips to get you started. <a name=\"prerequisites\"><\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_86 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a7a011bd9eb0\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a7a011bd9eb0\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/fr\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/fr\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#writing-a-csv-file\" >Writing a CSV file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/fr\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#reading-a-csv-file\" >Reading a CSV file<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"prerequisites\"><\/span>Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>-&gt; Knowledge of Python -&gt; Python 2.4 Distribution <a name=\"writing-a-csv-file\"><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"writing-a-csv-file\"><\/span>Writing a CSV file<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Start by importing the CSV module:<\/p>\n<pre>import csv<\/pre>\n<p>We will define an object \u00ab\u00a0writer\u00a0\u00bb (named c), which can later be used to write the CSV file.<\/p>\n<pre>c = csv.writer(open(\"MYFILE.csv\", \"wb\"))<\/pre>\n<p>Now we will apply the method to write a row. Writerow The method takes one argument &#8211; this argument must be a list and each list item is equivalent to a column. Here, we try to make an address book:<\/p>\n<pre>c.writerow([\"Name\",\"Address\",\"Telephone\",\"Fax\",\"E-mail\",\"Others\"])<\/pre>\n<p>Then we will save all entries in this way. <a name=\"reading-a-csv-file\"><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"reading-a-csv-file\"><\/span>Reading a CSV file<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First just create an object reader (we will give it a name: cr).<\/p>\n<pre>cr = csv.reader(open(\"MYFILE.csv\",\"rb\"))<\/pre>\n<p>And here we get each row (in the form of a list of columns) as follows:<\/p>\n<pre>for row in cr:    \n    print row<\/pre>\n<p>We can of course extract a precise entry of a row with the index:<\/p>\n<pre>for row in reader:    \n    print row[2], row[-2]<\/pre>\n<p>","protected":false},"excerpt":{"rendered":"<p>[dropcap]I[\/dropcap]t is possible to&nbsp;read and write CSV (comma separated values) files&nbsp;using Python 2.4 Distribution. Like most languages, file operations can be done with Python.&nbsp;Writing a CSV file&nbsp;with Python can be&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3092,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[196,122],"tags":[756,683,123,487,486],"class_list":["post-3079","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-file-type","category-python","tag-create-a-file","tag-csv","tag-python-language","tag-reading-a-csv-file","tag-writing-a-csv-file"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=3079"}],"version-history":[{"count":13,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3079\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3079\/revisions\/3098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/3092"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=3079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=3079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=3079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}