PYTHON CSV READER SKIP HEADER

Summary: in this tutorial, you’ll learn exactly how to review a CSV file in Python making use of the integrated csv module.

You watching: Python csv reader skip header

What is a CSV file

CSV stands for comma-separated worths. A CSV file is a derestricted text file that uses a comma to separate worths.

A CSV file consists of one or even more lines. Each line is a file document. And each information record is composed of one or more worths separated by commas. In enhancement, all the lines of a CSV file have the exact same variety of worths.

Typically, you use a CSV file to keep tabular information in plain message. The CSV file format is rather renowned and sustained by many software applications such as Microsoft Excel and also Google Spreadsheet.

*

Reading a csv file in Python

To review a CSV file in Python, you follow these steps:

First, import the csv module:


import csv
Code language: Python (python)2nd, open up the CSV file using the built-in open() feature in the check out mode:


f = open("path/to/csv_file")
Code language: Python (python)If the CSV consists of UTF8 personalities, you should specify the encoding prefer this:


f = open("path/to/csv_file", encoding="UTF8")
Code language: Python (python)Third, pass the file object (f) to the reader() feature of the csv module. The reader() function retransforms a csv reader object:


csv_reader = csv.reader(f)
Code language: Python (python)The csv_reader is an iterable object of lines from the CSV file. As such, you have the right to iterate over the lines of the CSV file utilizing a for loop:


for line in csv_reader: print(line)
Code language: Python (python)Each line is a list of values. To access each value, you use the square bracket notation <>. The first value has an index of 0. The second worth has an index of 1, and also so on.

For example, the adhering to accesses the first worth of a certain line:


line<0>
Code language: Python (python)Finally, constantly cshed the file when you’re no much longer accessibility it by calling the close() method of the file object:


f.close()
Code language: Python (python)It’ll be easier to usage the through statement so that you don’t have to clearly speak to the close() approach.

The adhering to illustprices all the measures for reading a CSV file:


import csvvia open("path/to/csv_file", "r") as f: csv_reader = csv.reader(f) for line in csv_reader: # procedure each line print(line)
Code language: Python (python)

Reading a CSV file examples

We’ll use the nation.csv file that has nation indevelopment consisting of name, location, 2-letter country code, 3-letter nation code:

*
Downfill country.csv file

The adhering to reflects exactly how to check out the nation.csv file and screen each line to the screen:


import csvthrough open("country.csv", encoding="utf8") as f: csv_reader = csv.reader(f) for line in csv_reader: print(line)
Code language: Python (python)Output:


<"name", "area", "country_code2", "country_code3"><"Afghanistan", "652090.00", "AF", "AFG"><"Albania", "28748.00", "AL", "ALB"><"Algeria", "2381741.00", "DZ", "DZA"><"American Samoa", "199.00", "AS", "ASM">...
Code language: Python (python)The nation.csv has actually the first line as the header. To sepaprice the header and information, you usage the enumerate() feature to acquire the index of each line:


import csvthrough open("nation.csv", encoding="utf8") as f: csv_reader = csv.reader(f) for line_no, line in enumerate(csv_reader, 1): if line_no == 1: print("Header:") print(line) # header print("Data:") else: print(line) # data
Code language: Python (python)In this instance, we use the enumerate() attribute and specify the index of the first line as 1.

See more: Prentice Hall Review Book Reviews : Population Dilemma, Edited By Philip M

Inside the loop, if the line_no is is 1, the line is the header. Otherwise, it’s a file line.

Another method to skip the header is to use the next() feature. The next() function forwards to the reader to the following line. For example:


import csvwith open("country.csv", encoding="utf8") as f: csv_reader = csv.reader(f) # skip the first row next(csv_reader) # present the information for line in csv_reader: print(line)
Code language: Python (python)The following reads the nation.csv file and also calculate the complete locations of all countries:


import csvtotal_location = 0# calculate the full location of all countriesthrough open("country.csv", encoding="utf8") as f: csv_reader = csv.reader(f) # skip the header next(csv_reader) # calculate complete for line in csv_reader: total_location += float(line<1>)print(total_area)
Code language: Python (python)Output:


148956306.9
Code language: Python (python)

Reading a CSV file using the DictReader class

When you usage the csv.reader() function, you have the right to accessibility worths of the CSV file making use of the bracket notation such as line<0>, line<1>, and so on. However before, making use of the csv.reader() feature has actually 2 primary limitations:

First, the way to accessibility the worths from the CSV file is not so obvious. For instance, the line<0> implicitly means the nation name. It would certainly be even more expressive if you have the right to accessibility the country name prefer line<"country_name">.Second, once the order of columns from the CSV file is changed or brand-new columns are included, you need to modify the code to obtain the best data.

This is wbelow the DictReader class comes right into play. The DictReader course likewise originates from the csv module.

The DictReader course permits you to develop an item favor a continual CSV reader. But it maps the information of each line to a dictionary (dict) whose secrets are specified by the values of the first line.

By making use of the DictReader class, you can accessibility values in the country.csv file favor line<"name">, line<"area">, line<"country_code2">, and also line<"country_code3">.

The adhering to example uses the DictReader course to check out the nation.csv file:


import csvthrough open("country.csv", encoding="utf8") as f: csv_reader = csv.DictReader(f) # skip the header next(csv_reader) # show the information for line in csv_reader: print(f"The location of line<"name"> is line<"area"> km2")
Code language: Python (python)Output:


The area of Afghanistan is 652090.00 km2The location of Albania is 28748.00 km2The area of Algeria is 2381741.00 km2 ...
Code language: Python (python)If you desire to have actually different area names various other than the ones mentioned in the first line, you have the right to clearly specify them by passing a list of field names to the DictReader() constructor choose this:


import csvfieldnames = <"country_name", "area", "code2", "code3">with open("nation.csv", encoding="utf8") as f: csv_reader = csv.DictReader(f, fieldnames) next(csv_reader) for line in csv_reader: print(f"The area of line<"country_name"> is line<"area"> km2")
Code language: Python (python)In this instance, instead of making use of worths from the first line as the area names, we explicitly pass a list of field names to the DictReader constructor.

Summary

Use csv.reader() feature or csv.DictReader course to review information from a CSV file.
Did you find this tutorial valuable ?

Yes No