LoginSignup
4
1

More than 3 years have passed since last update.

【Python】データサイズが大きいcsvファイルをgeneratorを使って読み込む

Last updated at Posted at 2020-02-12

import csv


def csv_reader(file, header=False):
    with open(file, "r") as f:
        reader = csv.reader(f)

        if header:
            next(reader)

        yield from reader


def main():
    g = csv_reader("xxx.csv", header=False)
    print(next(g)) # => 一行目

if __name__ == '__main__':
    main()




yield from reader


for row in reader:
    yield row

でも実装できます。

4
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1