開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成: 1つのことだけをうまくやる)、自分で考えてみよう(p.139)をpythonで考えてみる。
自分で考えてみよう(p.139)
コード(BBEdit, Emacs)
sample139.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- with open('spooky.csv') as in_f, open('ufos.csv', 'w') as f1, \ open('disappearances.csv', 'w') as f2, open('others.csv', 'w') as f3: for line in in_f: line = line.rstrip() if line.find('UFO') != -1: print(line, file=f1) elif line.find('Disappearance') != -1: print(line, file=f2) else: print(line, file=f3)
入出力結果(Terminal, IPython)
$ ./sample139.py $ cat spooky.csv 30.685163,-68.137207,Type=Yeti -100.0, 50,Type=Test1 28.304380,-74.575195,Type=UFO 29.132971,-71.136475,Type=Ship 28.343065,-62.753906,Type=Elvis 27.868217,-68.005371,Type=Goatsucker 30.496017,-73.333740,Type=Disappearance 26.224447,-71.477051,Type=UFO 29.401320,-66.027832,Type=Ship 37.879536,-69.477539,Type=Elvis 22.705256,-68.192139,Type=Elvis 27.166695,-87.484131,Type=Elvis 50,200,Type=Test2 $ cat ufos.csv 28.304380,-74.575195,Type=UFO 26.224447,-71.477051,Type=UFO $ cat disappearances.csv 30.496017,-73.333740,Type=Disappearance $ cat others.csv 30.685163,-68.137207,Type=Yeti -100.0, 50,Type=Test1 29.132971,-71.136475,Type=Ship 28.343065,-62.753906,Type=Elvis 27.868217,-68.005371,Type=Goatsucker 29.401320,-66.027832,Type=Ship 37.879536,-69.477539,Type=Elvis 22.705256,-68.192139,Type=Elvis 27.166695,-87.484131,Type=Elvis 50,200,Type=Test2 $
0 コメント:
コメントを投稿