Pynote

Python、機械学習、画像処理について

Python - CSV の読み込み/書き込み まとめ

サンプルとして使用する CSV

"Name","Sex","Age","Height (in)","Weight (lbs)"
"Alex","M",41,74,170
"Bert","M",42,68,166
"Carl","M",32,70,155
"Dave","M",39,72,167
"Elly","F",30,66,124
"Fran","F",33,66,115
"Gwen","F",26,64,121
"Hank","M",30,71,158
"Ivan","M",53,72,175
"Jake","M",32,69,143
"Kate","F",47,69,139
"Luke","M",34,72,163
"Myra","F",23,62,98
"Neil","M",36,75,160
"Omar","M",38,70,145
"Page","F",31,67,135
"Quin","M",29,71,176
"Ruth","F",28,65,131

CSV を読み込み、リストで取得する。

1行ずつ読み込む。

import csv

with open('test.csv', newline='') as f:
    reader = csv.reader(f)
    for line in reader:
        print(line)
['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)']
['Alex', 'M', '41', '74', '170']
['Bert', 'M', '42', '68', '166']
['Carl', 'M', '32', '70', '155']
['Dave', 'M', '39', '72', '167']
['Elly', 'F', '30', '66', '124']

一括で読み込む。

import csv

with open('test.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

from pprint import pprint
pprint(data)
[['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)'],
 ['Alex', 'M', '41', '74', '170'],
 ['Bert', 'M', '42', '68', '166'],
 ['Carl', 'M', '32', '70', '155'],
 ['Dave', 'M', '39', '72', '167'],
 ['Elly', 'F', '30', '66', '124']]

ヘッダーをスキップする。

import csv

with open('test.csv', newline='') as f:
    reader = csv.reader(f)
    header = next(reader)  # ヘッダーをスキップする。
    print('header', header)
    for line in reader:  # 1行ずつ読み込む。
        print(line)
header ['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)']
['Alex', 'M', '41', '74', '170']
['Bert', 'M', '42', '68', '166']
['Carl', 'M', '32', '70', '155']
['Dave', 'M', '39', '72', '167']
['Elly', 'F', '30', '66', '124']

リストを CSV に保存する。

1行ずつ書き込む。

import csv

header = ['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)']
data = [['Alex', 'M', 41, 74, 170],
        ['Bert', 'M', 42, 68, 166],
        ['Elly', 'F', 30, 66, 124],
        ['Fran', 'F', 33, 66, 115]]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(header)
    for line in data:
        writer.writerow(line)

一括で書き込む。

import csv

header = ['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)']
data = [['Alex', 'M', 41, 74, 170],
        ['Bert', 'M', 42, 68, 166],
        ['Elly', 'F', 30, 66, 124],
        ['Fran', 'F', 33, 66, 115]]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(header)
    writer.writerows(data)

CSV を読み込み、OrderDict のリストで取得する。

1行ずつ読み込む。

import csv

with open('test.csv', newline='') as f:
    reader = csv.DictReader(f)
    for line in reader:  # 1行ずつ読み込む。
        print(line)
OrderedDict([('Name', 'Alex'), ('Sex', 'M'), ('Age', '41'), ('Height (in)', '74'), ('Weight (lbs)', '170')])
OrderedDict([('Name', 'Bert'), ('Sex', 'M'), ('Age', '42'), ('Height (in)', '68'), ('Weight (lbs)', '166')])
OrderedDict([('Name', 'Carl'), ('Sex', 'M'), ('Age', '32'), ('Height (in)', '70'), ('Weight (lbs)', '155')])
OrderedDict([('Name', 'Dave'), ('Sex', 'M'), ('Age', '39'), ('Height (in)', '72'), ('Weight (lbs)', '167')])
OrderedDict([('Name', 'Elly'), ('Sex', 'F'), ('Age', '30'), ('Height (in)', '66'), ('Weight (lbs)', '124')])

一括で読み込む。

import csv

with open('test.csv', newline='') as f:
    reader = csv.DictReader(f)
    data = list(reader)

from pprint import pprint
pprint(data, width=120)
[OrderedDict([('Name', 'Alex'), ('Sex', 'M'), ('Age', '41'), ('Height (in)', '74'), ('Weight (lbs)', '170')]),
 OrderedDict([('Name', 'Bert'), ('Sex', 'M'), ('Age', '42'), ('Height (in)', '68'), ('Weight (lbs)', '166')]),
 OrderedDict([('Name', 'Carl'), ('Sex', 'M'), ('Age', '32'), ('Height (in)', '70'), ('Weight (lbs)', '155')]),
 OrderedDict([('Name', 'Dave'), ('Sex', 'M'), ('Age', '39'), ('Height (in)', '72'), ('Weight (lbs)', '167')]),
 OrderedDict([('Name', 'Elly'), ('Sex', 'F'), ('Age', '30'), ('Height (in)', '66'), ('Weight (lbs)', '124')])]

dict のリストを CSV に保存する。


辞書は順序が規定されないため、fieldnames に辞書のキーのリストを渡すことで、列の順序を設定する必要がある。

1行ずつ書き込む。

import csv

header = ['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)']
data = [{'Name': 'Alex', 'Sex': 'M', 'Age': 41, 'Height (in)': 74, 'Weight (lbs)': 170},
        {'Name': 'Bert', 'Sex': 'M', 'Age': 42, 'Height (in)': 68, 'Weight (lbs)': 166},
        {'Name': 'Elly', 'Sex': 'F', 'Age': 30, 'Height (in)': 66, 'Weight (lbs)': 124},
        {'Name': 'Fran', 'Sex': 'F', 'Age': 33, 'Height (in)': 66, 'Weight (lbs)': 115}]

with open('output.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=header)
    for line in data:
        writer.writerow(line)

一括で書き込む。

import csv

header = ['Name', 'Sex', 'Age', 'Height (in)', 'Weight (lbs)']
data = [{'Name': 'Alex', 'Sex': 'M', 'Age': 41, 'Height (in)': 74, 'Weight (lbs)': 170},
        {'Name': 'Bert', 'Sex': 'M', 'Age': 42, 'Height (in)': 68, 'Weight (lbs)': 166},
        {'Name': 'Elly', 'Sex': 'F', 'Age': 30, 'Height (in)': 66, 'Weight (lbs)': 124},
        {'Name': 'Fran', 'Sex': 'F', 'Age': 33, 'Height (in)': 66, 'Weight (lbs)': 115}]

with open('output.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=header)
    writer.writerows(data)

書き込む際のクォーテーションの設定

すべてに二重引用符を付ける。

import csv

data = [['Alex" Jones', 'M', 41, 74, 170],
        ['Bert Meijer', 'M', 42, 68, 166],
        ['Elly Keinan', 'F', 30, 66, 124],
        ['Fran Drescher', 'F', 33, 66, 115]]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_ALL)
    writer.writerows(data)
"Alex"" Jones","M","41","74","170"
"Bert Meijer","M","42","68","166"
"Elly Keinan","F","30","66","124"
"Fran Drescher","F","33","66","115"

必要なものだけ二重引用符を付ける。

import csv

data = [['Alex" Jones', 'M', 41, 74, 170],
        ['Bert Meijer', 'M', 42, 68, 166],
        ['Elly Keinan', 'F', 30, 66, 124],
        ['Fran Drescher', 'F', 33, 66, 115]]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
    writer.writerows(data)
"Alex"" Jones",M,41,74,170
Bert Meijer,M,42,68,166
Elly Keinan,F,30,66,124
Fran Drescher,F,33,66,115

数字以外に二重引用符を付ける。

import csv

data = [['Alex" Jones', 'M', 41, 74, 170],
        ['Bert Meijer', 'M', 42, 68, 166],
        ['Elly Keinan', 'F', 30, 66, 124],
        ['Fran Drescher', 'F', 33, 66, 115]]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
    writer.writerows(data)
"Alex"" Jones","M",41,74,170
"Bert Meijer","M",42,68,166
"Elly Keinan","F",30,66,124
"Fran Drescher","F",33,66,115

二重引用符を付けない

import csv

data = [['Alex Jones', 'M', 41, 74, 170],
        ['Bert Meijer', 'M', 42, 68, 166],
        ['Elly Keinan', 'F', 30, 66, 124],
        ['Fran Drescher', 'F', 33, 66, 115]]

with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_NONE)
    writer.writerows(data)
Alex Jones,M,41,74,170
Bert Meijer,M,42,68,166
Elly Keinan,F,30,66,124
Fran Drescher,F,33,66,115