Home > Archives > 2008-06-17
2008-06-17
Python: CSVファイルの読み込み - csv.readerオブジェクト
- 2008-06-17 (火)
- Python
ソースコード
#!/usr/bin/python
# coding: UTF-8
# CSVファイルの読み込み
import csv
filename = "table01.csv"
csvfile = open(filename)
print csvfile
for row in csv.reader(csvfile):
print row # 1行のリスト
for elem in row:
print elem, # 行の中の要素
print
csvfile.close()
print csvfile
CSVファイル (table01.csv) (番号,名前)
1,aki 2,hiro 3,norika 4,kaede
実行結果
<open file 'table01.csv', mode 'r' at 0x01BECCC8> ['1', 'aki'] 1 aki ['2', 'hiro'] 2 hiro ['3', 'norika'] 3 norika ['4', 'kaede'] 4 kaede <closed file 'table01.csv', mode 'r' at 0x01BECCC8>
リファレンス
- Comments: 0
- Trackbacks: 2
Home > Archives > 2008-06-17