{"id":3221,"date":"2023-10-21T07:59:49","date_gmt":"2023-10-21T02:29:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=3221"},"modified":"2023-11-10T11:53:20","modified_gmt":"2023-11-10T06:23:20","slug":"python-read-a-csv-file-line-by-line-with-or-without-header","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/","title":{"rendered":"Python: Read a CSV file line by line with or without header"},"content":{"rendered":"

In this article, we will be learning about how to read a CSV file line by line with or without a header. Along with that, we will be learning how to select a specified column while iterating over a file.<\/p>\n

Let us take an example where we have a file named students.csv.<\/strong><\/em><\/p>\n

\n
Id,Name,Course,City,Session\r\n<\/span>21,Mark,Python,London,Morning\r\n<\/span>22,John,Python,Tokyo,Evening\r\n<\/span>23,Sam,Python,Paris,Morning\r\n<\/span>32,Shaun,Java,Tokyo,Morning<\/span><\/pre>\n<\/div>\n
<\/div>\n
What we want is to read all the rows of this file line by line.<\/div>\n
Note that, we will not be reading this CSV file into lists of lists because that will be very space-consuming and time-consuming. It will also cause problems with the large data. We have to look for a solution that works as an interpreter where we can read a line one at a time so that less memory consumption will take place.<\/div>\n
<\/div>\n
Let’s get started with it!<\/em><\/div>\n
In python, we have two modules to read the CSV file, one is csv.reader<\/em><\/strong> and the second is csv.DictReader. <\/em><\/strong>We will use them one by one to read a CSV file line by line.<\/div>\n
<\/div>\n
\n

Read a CSV file line by line using csv.reader<\/h2>\n

By using the csv.reader\u00a0<\/em>module, a reader class object is made through which we can iterate over the lines of a CSV file as a list of values, where each value in the list is a cell value.<\/p>\n

\"Read<\/p>\n

Code:<\/p>\n

\n
from<\/span> csv <\/span>import<\/span> reader\r\n<\/span># open file in read mode\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span> # pass the file object to reader() to get the reader object\r\ncsv_reader = reader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span># Iterate over each row in the csv using reader object\r\nfor row <\/span>in<\/span> csv_reader:\r\n<\/span># row variable is a list that represents a row in csv\r\nprint(<\/span>row<\/span>)<\/span><\/pre>\n<\/div>\n<\/div>\n
The above code iterated over each row of the CSV file. It fetched the content of each row as a list and printed that generated list.<\/div>\n

How did it work?<\/h3>\n

It performed a few steps:<\/p>\n

    \n
  1. Opened the students.csv<\/em> file and created a file object.<\/li>\n
  2. In csv.reader() function, the reader object is created and passed.<\/li>\n
  3. Now with the reader object, we iterated it by using the for loop so that it can read each row of the csv as a list of values.<\/li>\n
  4. At last, we printed this list.<\/li>\n<\/ol>\n

    By using this module, only one line will consume memory at a time while iterating through a csv file.<\/p>\n

    Read csv file without header<\/h2>\n

    What if we want to skip a header and print the files without the header. In the previous example, we printed the values including the header but in this example, we will remove the header and print the values without the header.<\/p>\n

    \"Read<\/p>\n

    Code:<\/p>\n

    \n
    from<\/span> csv <\/span>import<\/span> reader\r\n<\/span># skip first line i.e. read header first and then iterate over each row od csv as a list\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span>csv_reader = reader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span>header = next<\/span>(<\/span>csv_reader<\/span>)\r\n<\/span># Check file as empty\r\nif header != <\/span>None<\/span>:\r\n<\/span># Iterate over each row after the header in the csv\r\nfor row <\/span>in<\/span> csv_reader:\r\n<\/span># row variable is a list that represents a row in csv\r\nprint(<\/span>row<\/span>)<\/span><\/pre>\n<\/div>\n
    We can see in the image above, that the header is not printed and the code is designed in such a way that it skipped the header and printed all the other values in a list.<\/div>\n
    \n

    Read csv file line by line using csv module DictReader object<\/h2>\n

    Now, we will see the example using csv.DictReader module. CSV’s module dictReader object class iterates over the lines of a CSV file as a dictionary, which means for each row it returns a dictionary containing the pair of column names and values for that row.<\/p>\n

    \"Read<\/p>\n

    Code:<\/p>\n

    \n
    from<\/span> csv <\/span>import<\/span> DictReader\r\n<\/span># open file in read mode\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span># pass the file object to DictReader() to get the DictReader object\r\ncsv_dict_reader = DictReader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span># iterate over each line as a ordered dictionary\r\nfor row <\/span>in<\/span> csv_dict_reader:\r\n<\/span># row variable is a dictionary that represents a row in csv\r\nprint(<\/span>row<\/span>)<\/span><\/pre>\n<\/div>\n<\/div>\n
    The above code iterated over all the rows of the CSV file. It fetched the content of the row for each row and put it as a dictionary.<\/div>\n

    How did it work?<\/h3>\n

    It performed a few steps:<\/p>\n

      \n
    1. Opened the students.csv<\/em> file and created a file object.<\/li>\n
    2. In csv.DictReader() function, the reader object is created and passed.<\/li>\n
    3. Now with the reader object, we iterated it by using the for loop so that it can read each row of the csv as a dictionary of values. Where each pair in this dictionary represents contains the column name & column value for that row.<\/li>\n<\/ol>\n

      It also saves the memory as only one row at a time is in the memory.<\/p>\n

      Get column names from the header in the CSV file<\/h2>\n

      We have a member function in the DictReader class that returns the column names of a csv file as a list.<\/p>\n

      \"GetCode:<\/p>\n

      from<\/span> csv <\/span>import<\/span> DictReader<\/span><\/div>\n
      # open file in read mode<\/span><\/div>\n
      with<\/span> open<\/span>(<\/span>‘students.csv’<\/span>, <\/span>‘r’<\/span>)<\/span> as<\/span> read_obj:<\/span><\/div>\n
      # pass the file object to DictReader() to get the DictReader object<\/span><\/div>\n
      csv_dict_reader = <\/span>DictReader<\/span>(<\/span>read_obj<\/span>)<\/span><\/div>\n
      # get column names from a csv file<\/span><\/div>\n
      column_names = csv_dict_reader.fieldname<\/span><\/div>\n
      print<\/span>(<\/span>column_names<\/span>)<\/span><\/div>\n
      \n

      Read specific columns from a csv file while iterating line by line<\/h2>\n

      Read specific columns (by column name) in a CSV file while iterating row by row<\/h4>\n<\/div>\n
      \n
      We will iterate over all the rows of the CSV file line by line but will print only two columns of each row.<\/div>\n
      \"Read<\/div>\n
      Code:<\/div>\n
      \n
      \n
      from<\/span> csv <\/span>import<\/span> DictReader\r\n<\/span># iterate over each line as a ordered dictionary and print only few column by column name\r\nwithopen<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span>as<\/span> read_obj:\r\n<\/span>csv_dict_reader = DictReader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span>for row <\/span>in<\/span> csv_dict_reader:\r\n<\/span>print(<\/span>row<\/span>[<\/span>'Id'<\/span>]<\/span>, row<\/span>[<\/span>'Name'<\/span>])<\/span><\/pre>\n<\/div>\n
      <\/div>\n
      DictReader returns a dictionary for each line during iteration. As in this dictionary, keys are column names and values are cell values for that column. So, for selecting specific columns in every row, we used column name with the dictionary object.<\/div>\n<\/div>\n

      Read specific columns (by column Number) in a CSV file while iterating row by row<\/h4>\n

      We will iterate over all the rows of the CSV file line by line but will print the contents of the 2nd and 3rd column.<\/p>\n

      \"Read<\/p>\n

      Code:<\/p>\n

      \n
      from<\/span> csv <\/span>import<\/span> reader\r\n<\/span># iterate over each line as a ordered dictionary and print only few column by column Number\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span>csv_reader = reader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span>for row <\/span>in<\/span> csv_reader:\r\n<\/span>print(<\/span>row<\/span>[<\/span>1<\/span>]<\/span>, row<\/span>[<\/span>2<\/span>])<\/span><\/pre>\n<\/div>\n<\/div>\n
      <\/div>\n
      With the csv.reader each row of the csv file is fetched as a list of values, where each value represents a column value. So, selecting the 2nd & 3rd column for each row, select elements at index 1 and 2 from the list.<\/div>\n
      <\/div>\n
      The complete code:<\/strong><\/em><\/div>\n
      <\/div>\n
      \n
      \n
      from<\/span> csv <\/span>import<\/span> reader\r\n<\/span>from csv<\/span> import<\/span> DictReader\r\n<\/span>def main<\/span>()<\/span>:\r\n<\/span>print(<\/span>'*** Read csv file line by line using csv module reader object ***'<\/span>)\r\n<\/span>print(<\/span>'*** Iterate over each row of a csv file as list using reader object ***'<\/span>)\r\n<\/span># open file in read mode\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span># pass the file object to reader() to get the reader object\r\ncsv_reader = reader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span># Iterate over each row in the csv using reader object\r\nfor row <\/span>in<\/span> csv_reader:\r\n<\/span># row variable is a list that represents a row in csv\r\nprint(<\/span>row<\/span>)\r\n<\/span>print(<\/span>'*** Read csv line by line without header ***'<\/span>)\r\n<\/span># skip first line i.e. read header first and then iterate over each row od csv as a list\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span>csv_reader = reader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span>header = next<\/span>(<\/span>csv_reader<\/span>)\r\n<\/span># Check file as empty\r\nif header != <\/span>None<\/span>:\r\n<\/span># Iterate over each row after the header in the csv\r\nfor row <\/span>in<\/span> csv_reader:\r\n<\/span># row variable is a list that represents a row in csv\r\nprint(<\/span>row<\/span>)\r\n<\/span>print(<\/span>'Header was: '<\/span>)\r\n<\/span>print(<\/span>header<\/span>)\r\n<\/span>print(<\/span>'*** Read csv file line by line using csv module DictReader object ***'<\/span>)\r\n<\/span># open file in read mode\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span># pass the file object to DictReader() to get the DictReader object\r\ncsv_dict_reader = DictReader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span># iterate over each line as a ordered dictionary\r\nfor row <\/span>in<\/span> csv_dict_reader:\r\n<\/span># row variable is a dictionary that represents a row in csv\r\nprint(<\/span>row<\/span>)\r\n<\/span>print(<\/span>'*** select elements by column name while reading csv file line by line ***'<\/span>)\r\n<\/span># open file in read mode\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span># pass the file object to DictReader() to get the DictReader object\r\ncsv_dict_reader = DictReader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span># iterate over each line as a ordered dictionary\r\nfor row <\/span>in<\/span> csv_dict_reader:\r\n<\/span># row variable is a dictionary that represents a row in csv\r\nprint(<\/span>row<\/span>[<\/span>'Name'<\/span>]<\/span>, <\/span>' is from '<\/span> , row<\/span>[<\/span>'City'<\/span>]<\/span> , <\/span>' and he is studying '<\/span>, row<\/span>[<\/span>'Course'<\/span>])\r\n<\/span>print(<\/span>'*** Get column names from header in csv file ***'<\/span>)\r\n<\/span># open file in read mode\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span># pass the file object to DictReader() to get the DictReader object\r\ncsv_dict_reader = DictReader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span># get column names from a csv file\r\ncolumn_names = csv_dict_reader.fieldnames\r\nprint(<\/span>column_names<\/span>)\r\n<\/span>print(<\/span>'*** Read specific columns from a csv file while iterating line by line ***'<\/span>)\r\n<\/span>print(<\/span>'*** Read specific columns (by column name) in a csv file while iterating row by row ***'<\/span>)\r\n<\/span># iterate over each line as a ordered dictionary and print only few column by column name\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span>csv_dict_reader = DictReader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span>for row <\/span>in<\/span> csv_dict_reader:\r\n<\/span>print(<\/span>row<\/span>[<\/span>'Id'<\/span>]<\/span>, row<\/span>[<\/span>'Name'<\/span>])\r\n<\/span>print(<\/span>'*** Read specific columns (by column Number) in a csv file while iterating row by row ***'<\/span>)\r\n<\/span># iterate over each line as a ordered dictionary and print only few column by column Number\r\nwith open<\/span>(<\/span>'students.csv'<\/span>, <\/span>'r'<\/span>)<\/span> as<\/span> read_obj:\r\n<\/span>csv_reader = reader<\/span>(<\/span>read_obj<\/span>)\r\n<\/span>for row <\/span>in<\/span> csv_reader:\r\n<\/span>print(<\/span>row<\/span>[<\/span>1<\/span>]<\/span>, row<\/span>[<\/span>2<\/span>])\r\n<\/span>if __name__<\/span> == <\/span>'__main__'<\/span>:\r\n<\/span>main()<\/span><\/pre>\n

      I hope you understood this article as well as the code.<\/p>\n

      Happy reading!<\/strong><\/em><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

      In this article, we will be learning about how to read a CSV file line by line with or without a header. Along with that, we will be learning how to select a specified column while iterating over a file. Let us take an example where we have a file named students.csv. Id,Name,Course,City,Session 21,Mark,Python,London,Morning 22,John,Python,Tokyo,Evening …<\/p>\n

      Python: Read a CSV file line by line with or without header<\/span> Read More »<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython: Read a CSV file line by line with or without header - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Read a CSV file line by line with or without header - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this article, we will be learning about how to read a CSV file line by line with or without a header. Along with that, we will be learning how to select a specified column while iterating over a file. Let us take an example where we have a file named students.csv. Id,Name,Course,City,Session 21,Mark,Python,London,Morning 22,John,Python,Tokyo,Evening … Python: Read a CSV file line by line with or without header Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-21T02:29:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:23:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bahija Siddiqui\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#primaryimage\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png\",\"width\":1288,\"height\":287,\"caption\":\"Read a CSV file line by line using csv.reader\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/\",\"name\":\"Python: Read a CSV file line by line with or without header - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#primaryimage\"},\"datePublished\":\"2023-10-21T02:29:49+00:00\",\"dateModified\":\"2023-11-10T06:23:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: Read a CSV file line by line with or without header\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7442c33f5d282892c551affb0de5cd16\"},\"headline\":\"Python: Read a CSV file line by line with or without header\",\"datePublished\":\"2023-10-21T02:29:49+00:00\",\"dateModified\":\"2023-11-10T06:23:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#webpage\"},\"wordCount\":875,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7442c33f5d282892c551affb0de5cd16\",\"name\":\"Bahija Siddiqui\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/978ea39d7d45a7b9c5e7e7b141d5b94b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/978ea39d7d45a7b9c5e7e7b141d5b94b?s=96&d=mm&r=g\",\"caption\":\"Bahija Siddiqui\"},\"url\":\"https:\/\/python-programs.com\/author\/bahija\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Read a CSV file line by line with or without header - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/","og_locale":"en_US","og_type":"article","og_title":"Python: Read a CSV file line by line with or without header - Python Programs","og_description":"In this article, we will be learning about how to read a CSV file line by line with or without a header. Along with that, we will be learning how to select a specified column while iterating over a file. Let us take an example where we have a file named students.csv. Id,Name,Course,City,Session 21,Mark,Python,London,Morning 22,John,Python,Tokyo,Evening … Python: Read a CSV file line by line with or without header Read More »","og_url":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-21T02:29:49+00:00","article_modified_time":"2023-11-10T06:23:20+00:00","og_image":[{"url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png"}],"twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Bahija Siddiqui","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#primaryimage","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png","width":1288,"height":287,"caption":"Read a CSV file line by line using csv.reader"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#webpage","url":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/","name":"Python: Read a CSV file line by line with or without header - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#primaryimage"},"datePublished":"2023-10-21T02:29:49+00:00","dateModified":"2023-11-10T06:23:20+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python: Read a CSV file line by line with or without header"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/7442c33f5d282892c551affb0de5cd16"},"headline":"Python: Read a CSV file line by line with or without header","datePublished":"2023-10-21T02:29:49+00:00","dateModified":"2023-11-10T06:23:20+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#webpage"},"wordCount":875,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"image":{"@id":"https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#primaryimage"},"thumbnailUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/04\/Screenshot-375.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-read-a-csv-file-line-by-line-with-or-without-header\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/7442c33f5d282892c551affb0de5cd16","name":"Bahija Siddiqui","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/978ea39d7d45a7b9c5e7e7b141d5b94b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/978ea39d7d45a7b9c5e7e7b141d5b94b?s=96&d=mm&r=g","caption":"Bahija Siddiqui"},"url":"https:\/\/python-programs.com\/author\/bahija\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3221"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3221"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3221\/revisions"}],"predecessor-version":[{"id":3346,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3221\/revisions\/3346"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}