{"id":743,"date":"2020-10-24T09:10:51","date_gmt":"2020-10-24T09:10:51","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=743"},"modified":"2025-03-30T12:12:20","modified_gmt":"2025-03-30T12:12:20","slug":"python-read-csv-file","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-read-csv-file\/","title":{"rendered":"Python Read CSV File"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to read a CSV file in Python using the built-in <code>csv<\/code> module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-a-csv-file'>What is a CSV file <a href=\"#what-is-a-csv-file\" class=\"anchor\" id=\"what-is-a-csv-file\" title=\"Anchor for What is a CSV file\">#<\/a><\/h2>\n\n\n\n<p>CSV stands for comma-separated values. A CSV file is a delimited text file that uses a comma to separate values.<\/p>\n\n\n\n<p>A CSV file consists of one or more lines. Each line is a data record. And each data record consists of one or more values separated by commas. In addition, all the lines of a CSV file have the same number of values.<\/p>\n\n\n\n<p>Typically, you use a CSV file to store tabular data in plain text. The CSV file format is quite popular and supported by many software applications such as Microsoft Excel and Google Spreadsheet.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"243\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Read-CSV-File.png\" alt=\"\" class=\"wp-image-751\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Read-CSV-File.png 584w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Read-CSV-File-300x125.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='reading-a-csv-file-in-python'>Reading a csv file in Python <a href=\"#reading-a-csv-file-in-python\" class=\"anchor\" id=\"reading-a-csv-file-in-python\" title=\"Anchor for Reading a csv file in Python\">#<\/a><\/h2>\n\n\n\n<p>To read a CSV file in Python, you follow these steps:<\/p>\n\n\n\n<p>First, import the csv module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, open the CSV file using the built-in open() function in the read mode:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f = open(<span class=\"hljs-string\">'path\/to\/csv_file'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the CSV contains UTF8 characters, you need to specify the encoding like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f = open(<span class=\"hljs-string\">'path\/to\/csv_file'<\/span>, encoding=<span class=\"hljs-string\">'UTF8'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, pass the file object (<code>f<\/code>) to the <code>reader()<\/code> function of the <code>csv<\/code> module. The <code>reader()<\/code> function returns a csv reader object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">csv_reader = csv.reader(f)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>csv_reader<\/code> is an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-iterables\/\">iterable<\/a> object of lines from the CSV file. Therefore, you can iterate over the lines of the CSV file using a <code>for<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n    print(line)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each line is a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-list\/\">list<\/a> of values. To access each value, you use the square bracket notation <code>[]<\/code>. The first value has an index of 0. The second value has an index of 1, and so on.<\/p>\n\n\n\n<p>For example, the following accesses the first value of a particular line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">line&#91;<span class=\"hljs-number\">0<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, always close the file once you&#8217;re no longer access it by calling the <code>close()<\/code> method of the file object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f.close()    <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;ll be easier to use the <code>with<\/code> statement so that you don&#8217;t need to explicitly call the <code>close()<\/code> method.<\/p>\n\n\n\n<p>The following illustrates all the steps for reading a CSV file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'path\/to\/csv_file'<\/span>, <span class=\"hljs-string\">'r'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        <span class=\"hljs-comment\"># process each line<\/span>\n        print(line)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='reading-a-csv-file-examples'>Reading a CSV file examples <a href=\"#reading-a-csv-file-examples\" class=\"anchor\" id=\"reading-a-csv-file-examples\" title=\"Anchor for Reading a CSV file examples\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>country.csv<\/code> file that contains country information including name, area, 2-letter country code, 3-letter country code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"254\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Read-CSV-File-Example.png\" alt=\"Python Read CSV File Example\" class=\"wp-image-752\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Read-CSV-File-Example.png 650w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-Read-CSV-File-Example-300x117.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n\n<a href=\"https:\/\/www.pythontutorial.net\/country\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"buttonDownload\">Download country.csv file<\/a>\n\n\n\n<p>The following shows how to read the <code>country.csv<\/code> file and display each line to the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">\"utf8\"<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        print(line)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&#91;<span class=\"hljs-string\">'name'<\/span>, <span class=\"hljs-string\">'area'<\/span>, <span class=\"hljs-string\">'country_code2'<\/span>, <span class=\"hljs-string\">'country_code3'<\/span>]\n&#91;<span class=\"hljs-string\">'Afghanistan'<\/span>, <span class=\"hljs-string\">'652090.00'<\/span>, <span class=\"hljs-string\">'AF'<\/span>, <span class=\"hljs-string\">'AFG'<\/span>]\n&#91;<span class=\"hljs-string\">'Albania'<\/span>, <span class=\"hljs-string\">'28748.00'<\/span>, <span class=\"hljs-string\">'AL'<\/span>, <span class=\"hljs-string\">'ALB'<\/span>]\n&#91;<span class=\"hljs-string\">'Algeria'<\/span>, <span class=\"hljs-string\">'2381741.00'<\/span>, <span class=\"hljs-string\">'DZ'<\/span>, <span class=\"hljs-string\">'DZA'<\/span>]\n&#91;<span class=\"hljs-string\">'American Samoa'<\/span>, <span class=\"hljs-string\">'199.00'<\/span>, <span class=\"hljs-string\">'AS'<\/span>, <span class=\"hljs-string\">'ASM'<\/span>]\n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>country.csv<\/code> has the first line as the header. To separate the header and data, you use the <code>enumerate()<\/code> function to get the index of each line:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">\"utf8\"<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n    <span class=\"hljs-keyword\">for<\/span> line_no, line <span class=\"hljs-keyword\">in<\/span> enumerate(csv_reader, <span class=\"hljs-number\">1<\/span>):\n        <span class=\"hljs-keyword\">if<\/span> line_no == <span class=\"hljs-number\">1<\/span>:\n            print(<span class=\"hljs-string\">'Header:'<\/span>)\n            print(line)  <span class=\"hljs-comment\"># header<\/span>\n            print(<span class=\"hljs-string\">'Data:'<\/span>)\n        <span class=\"hljs-keyword\">else<\/span>:\n            print(line)  <span class=\"hljs-comment\"># data<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we use the <code>enumerate()<\/code> function and specify the index of the first line as 1.<\/p>\n\n\n\n<p>Inside the loop, if the <code>line_no<\/code> is 1, the line is the header. Otherwise, it&#8217;s a data line.<\/p>\n\n\n\n<p>Another way to skip the header is to use the <code>next()<\/code> function. The <code>next()<\/code> function forwards to the reader to the next line. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">\"utf8\"<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n\n    <span class=\"hljs-comment\"># skip the first row<\/span>\n    next(csv_reader)\n\n    <span class=\"hljs-comment\"># show the data<\/span>\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        print(line)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following reads the <code>country.csv<\/code> file and calculate the total areas of all countries:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\ntotal_area = <span class=\"hljs-number\">0<\/span>\n\n<span class=\"hljs-comment\"># calculate the total area of all countries<\/span>\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">\"utf8\"<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n\n    <span class=\"hljs-comment\"># skip the header<\/span>\n    next(csv_reader)\n\n    <span class=\"hljs-comment\"># calculate total<\/span>\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        total_area += float(line&#91;<span class=\"hljs-number\">1<\/span>])\n\nprint(total_area)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">148956306.9<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='reading-a-csv-file-using-the-dictreader-class'>Reading a CSV file using the DictReader class <a href=\"#reading-a-csv-file-using-the-dictreader-class\" class=\"anchor\" id=\"reading-a-csv-file-using-the-dictreader-class\" title=\"Anchor for Reading a CSV file using the DictReader class\">#<\/a><\/h2>\n\n\n\n<p>When you use the <code>csv.reader()<\/code> function, you can access values of the CSV file using the bracket notation such as <code>line[0]<\/code>, <code>line[1]<\/code>, and so on. However, using the <code>csv.reader()<\/code> function has two main limitations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the way to access the values from the CSV file is not so obvious. For example, the <code>line[0]<\/code> implicitly means the country name. It would be more expressive if you can access the country name like <code>line['country_name']<\/code>.<\/li>\n\n\n\n<li>Second, when the order of columns from the CSV file is changed or new columns are added, you need to modify the code to get the right data.<\/li>\n<\/ul>\n\n\n\n<p>This is where the <code>DictReader<\/code> class comes into play. The DictReader class also comes from the <code>csv<\/code> module.<\/p>\n\n\n\n<p>The <code>DictReader<\/code> class allows you to create an object like a regular CSV reader. But it maps the information of each line to a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-dictionary\/\">dictionary<\/a> (<code>dict<\/code>) whose keys are specified by the values of the first line.<\/p>\n\n\n\n<p>By using the <code>DictReader<\/code> class, you can access values in the <code>country.csv<\/code> file like <code>line['name']<\/code>, <code>line['area']<\/code>, <code>line['country_code2']<\/code>, and line<code>['country_code3']<\/code>.<\/p>\n\n\n\n<p>The following example uses the <code>DictReader<\/code> class to read the <code>country.csv<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">\"utf8\"<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.DictReader(f)\n    <span class=\"hljs-comment\"># skip the header<\/span>\n    next(csv_reader)\n    <span class=\"hljs-comment\"># show the data<\/span>\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        print(<span class=\"hljs-string\">f\"The area of <span class=\"hljs-subst\">{line&#91;<span class=\"hljs-string\">'name'<\/span>]}<\/span> is <span class=\"hljs-subst\">{line&#91;<span class=\"hljs-string\">'area'<\/span>]}<\/span> km2\"<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">The area of Afghanistan <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-number\">652090.00<\/span> km2\nThe area of Albania <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-number\">28748.00<\/span> km2\nThe area of Algeria <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-number\">2381741.00<\/span> km2        \n...\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to have different field names other than the ones specified in the first line, you can explicitly specify them by passing a list of field names to the <code>DictReader()<\/code> constructor like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\nfieldnames = &#91;<span class=\"hljs-string\">'country_name'<\/span>, <span class=\"hljs-string\">'area'<\/span>, <span class=\"hljs-string\">'code2'<\/span>, <span class=\"hljs-string\">'code3'<\/span>]\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">\"utf8\"<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.DictReader(f, fieldnames)\n    next(csv_reader)\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        print(<span class=\"hljs-string\">f\"The area of <span class=\"hljs-subst\">{line&#91;<span class=\"hljs-string\">'country_name'<\/span>]}<\/span> is <span class=\"hljs-subst\">{line&#91;<span class=\"hljs-string\">'area'<\/span>]}<\/span> km2\"<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, instead of using values from the first line as the field names, we explicitly pass a list of field names to the <code>DictReader<\/code> constructor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>csv.reader()<\/code> function or <code>csv.DictReader<\/code> class to read data from a CSV file.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=read-csv\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"743\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-read-csv-file\/\"\n\t\t\t\tdata-post-title=\"Python Read CSV File\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"743\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-read-csv-file\/\"\n\t\t\t\tdata-post-title=\"Python Read CSV File\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn various ways to read a CSV file using the reader() function or DictReader class from the built-in csv module.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":69,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-743","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/743\/revisions"}],"predecessor-version":[{"id":7230,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/743\/revisions\/7230"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}