Last active
October 29, 2015 08:29
-
-
Save amano41/7ab04ae40f4d5e715150 to your computer and use it in GitHub Desktop.
Python で CSV ファイルの読み書き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import csv | |
with open("sample.csv", "r") as f: | |
reader = csv.reader(f) | |
header = next(reader) # ヘッダを読み飛ばす | |
for row in reader: # 1 行ずつ文字列のリストとして読み込む | |
print(",".join(row)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import csv | |
header = ["A", "B", "C"] | |
lines = [[r * 3 + c for c in range(3)] for r in range(3)] | |
with open("sample.csv", "w") as f: | |
writer = csv.writer(f) | |
writer.writerow(header) # 1 行ずつ出力 | |
writer.writerows(lines) # 複数行をまとめて出力 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment