LoginSignup
1
2

More than 3 years have passed since last update.

【Python】CSVファイルの読み込み

Last updated at Posted at 2020-09-09
csvfile.csv
Aさん,1,月
Bさん,2,火
Cさん,3,水
main.py
import csv

#ファイルの読み込みを行います
csv_file = open("csvfile.csv", "r", encoding="utf-8", errors="", newline="" )
print(type(csv_file))
"""
<class '_io.TextIOWrapper'>
"""

#CSV文字列をパースします。 
csvData = csv.reader(csv_file, delimiter=",", doublequote=True, lineterminator="\r\n", quotechar='"', skipinitialspace=True)
print(type(csvData))
"""
<class '_csv.reader'>
"""

#_csv.reader型をリストに変換
csvData = [row for row in csvData]
print(type(csvData))
print(csvData)
"""
<class 'list'>
[['Aさん', '1', '月'], ['Bさん', '2', '火'], ['Cさん', '3', '水']]
"""
1
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
1
2