{"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 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 <\/p>\n Code:<\/p>\n It performed a few steps:<\/p>\n By using this module, only one line will consume memory at a time while iterating through a csv file.<\/p>\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 <\/p>\n Code:<\/p>\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 <\/p>\n Code:<\/p>\n It performed a few steps:<\/p>\n It also saves the memory as only one row at a time is in the memory.<\/p>\n We have a member function in the DictReader class that returns the column names of a csv file as a list.<\/p>\n Code:<\/p>\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 <\/p>\n Code:<\/p>\nId,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
Read a CSV file line by line using csv.reader<\/h2>\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
How did it work?<\/h3>\n
\n
Read csv file without header<\/h2>\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
Read csv file line by line using csv module DictReader object<\/h2>\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
How did it work?<\/h3>\n
\n
Get column names from the header in the CSV file<\/h2>\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
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
Read specific columns (by column Number) in a CSV file while iterating row by row<\/h4>\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
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>