Skip to content

Instantly share code, notes, and snippets.

@amano41
Last active October 29, 2015 08:29
Show Gist options
  • Save amano41/7ab04ae40f4d5e715150 to your computer and use it in GitHub Desktop.
Save amano41/7ab04ae40f4d5e715150 to your computer and use it in GitHub Desktop.
Python で CSV ファイルの読み書き
#! /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))
#! /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