[Python] Basics of Reading CSV Files: Using csv.reader and Handling Headers

You can easily import CSV formatted data as lists using Python’s standard csv module.

This article explains the basic method of reading all lines and how to skip the first line (header) to process only the data.

目次

1. Reading the CSV File As-Is

By creating a csv.reader object and iterating through it with a for loop, you can retrieve each line as a “list of strings”.

Sample Data (products.csv)

ID,ProductName,Price
101,Apple,120
102,Orange,80
103,Banana,150

Source Code

import csv

# 1. Open the file
# It is recommended to specify newline="" when handling CSVs
# (This prevents automatic newline conversion issues across platforms)
with open("products.csv", encoding="utf-8", newline="") as f:
    
    # 2. Create Reader object
    reader = csv.reader(f)
    
    # 3. Read and print line by line
    print("--- Reading All Lines ---")
    for row in reader:
        # row becomes a list like ['ID', 'ProductName', 'Price']
        print(row)

Execution Result

--- Reading All Lines ---
['ID', 'ProductName', 'Price']
['101', 'Apple', '120']
['102', 'Orange', '80']
['103', 'Banana', '150']

2. How to Skip the Header Row

In data analysis, you often only need the data from the second line onwards, ignoring the item names in the first line.

In this case, you can skip the header (or save it to a variable) by using the next() function to advance the iterator by one.

Source Code

import csv

with open("products.csv", encoding="utf-8", newline="") as f:
    reader = csv.reader(f)
    
    # Skip the header (the first line)
    # The header row is returned, so save it to a variable if needed
    header = next(reader)
    
    print(f"Header: {header}")
    print("-" * 20)
    print("--- Processing Data Only ---")
    
    # Since the first line is already consumed, the loop starts from the second line
    for row in reader:
        product_name = row[1]
        price = row[2]
        print(f"{product_name} is {price} yen.")

Execution Result

Header: ['ID', 'ProductName', 'Price']
--------------------
--- Processing Data Only ---
Apple is 120 yen.
Orange is 80 yen.
Banana is 150 yen.

Explanation

newline=""

When opening a CSV with the open() function, if you do not specify this argument, issues such as extra blank lines may occur, particularly in Windows environments. It is best practice to always include it.

next(reader)

The reader object is an iterator (a mechanism to retrieve data sequentially). Calling next() once performs the action of “getting the current line and advancing the reading position to the next line.” As a result, the subsequent for loop starts processing automatically from the next line.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次