2
2

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 3 years have passed since last update.

pythonでcsvを読み込んで辞書型変数に格納

Posted at

#はじめに
pythonでcsvを読み込み、辞書型の変数に格納するコードです。

######環境
python3.9.6

#CSVの読み込み

######CSVファイル(test.csv)

1,2,3,4
apple,orange,banana,grape
りんご,おれんじ,ばなな,ぐれーぷ

######CSV読み込んで辞書型変数に格納するpythonコード


import csv

#CSVファイルを開く
with open("./test.csv", "r", encoding="utf-8") as csvfile:
    #CSVファイルを辞書型で読み込む
    f = csv.DictReader(csvfile, delimiter=",", doublequote=True, lineterminator="\r\n", quotechar='"', skipinitialspace=True)
    #辞書型で出力
    for row in f:
        print(row)

######出力結果

{'1': 'apple', '2': 'orange', '3': 'banana', '4': 'grape'}
{'1': 'りんご', '2': 'おれんじ', '3': 'ばなな', '4': 'ぐれーぷ'}
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?