開発環境
- macOS Mojave - Apple
- Emacs (Text Editor)
- Python 3.7 (プログラミング言語)
入門 Python 3 (Bill Lubanovic (著)、斎藤 康毅 (監修)、長尾 高弘 (翻訳)、オライリージャパン)の8章(データの行き先)、8.7(復習問題)8-3、4.を取り組んでみる。
コード(Emacs)
Python 3
#!/usr/bin/env python3
import csv
print('3.')
s = '''author,book
J R R Tolkien,The Hobbit
Lynne Truss,"Eats, Shoots & Leaves"'''
with open('books.csv', 'w') as f:
print(s, end='', file=f)
print('4.')
with open('books.csv') as f:
books = csv.DictReader(f)
for book in books:
print(book)
入出力結果(Terminal, Jupyter(IPython))
$ ./sample3.py
3.
4.
OrderedDict([('author', 'J R R Tolkien'), ('book', 'The Hobbit')])
OrderedDict([('author', 'Lynne Truss'), ('book', 'Eats, Shoots & Leaves')])
$
0 コメント:
コメントを投稿