0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonでCSVを読み込む方法

Posted at

#概要
PythonでCSVを読み込む方法について、調べたので共有します。

#事前準備
サンプルでは、下記のコードを記述した.pyファイルと同階層に、「test.csv」を配置。
中身は以下です。


aaaa,1,10,100
bbbb,2,20,200
cccc,3,30,300

#コード
上記のファイルを読み込むコードは以下になります。


# CSVを読み込むためのモジュールをインポート
import csv

# withを使ってopenすることで、closeし忘れを防ぐ
# このスクリプトと同階層にあるtest.csvを開く
with open("./test.csv", "r", encoding="utf-8") as f:
    reader = csv.reader(f)
    # rowにはcsvの一行分がlistで格納されている
    for row in reader:
        print(row)

#出力結果


['aaaa', '1', '10', '100']
['bbbb', '2', '20', '200']
['cccc', '3', '30', '300']

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?