Head First Programming
A learner's guide to programming
using the Python language
( O'Reilly Media; )
David Griffiths (著) Paul Barry (著)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Programming A learner's guide to programming using the Python language (David Griffiths(著)、Paul Barry(著)、 O'Reilly Media; )のChapter 5(hashes and databases - Putting data in its place)、Code Magnets(p.165)を解いてみる。
Code Magnets(p.165)
コード(BBEdit)
sample165.py
#!/usr/bin/env python3 #-*- coding: utf-8 def findDetails(id2find): with open('surfing_data.csv') as surfers_f: for each_line in surfers_f: s = {} s['id'], s['name'], s['country'], s['average'], s['board'], \ s['age'] = each_line.split(';') if id2find == int(s['id']): return s return {} if __name__ == '__main__': for id2find in [100, 105, 110, 200]: print(findDetails(id2find))
入出力結果(Terminal)
$ ./sample165.py {} {'id': '105', 'board': 'Fish', 'age': '24\n', 'country': 'Japan', 'name': "Aideen 'board babe' Wu", 'average': '8.65'} {} {} $
0 コメント:
コメントを投稿