開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の8章(ファイル処理)、8.8(練習問題)、2.を解いてみる。
8.8(練習問題)、2.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8
def readWeatherData(r):
fields = (((4, int), (2, int), (2, int)),
((2, int), (2, int), (2, int)),
((2, int), (2, int), (2, int)),
((6, float), (6, float), (6, float)))
result = []
for line in r:
start = 0
record = []
for field in fields:
group = []
for width, target_type in field:
text = line[start:start+width]
value = target_type(text)
group.append(value)
start += width
record.append(tuple(group))
result.append(tuple(record))
return result
with open('weather.txt') as f:
records = readWeatherData(f)
for record in records:
print(record)
入出力結果(Terminal)
$ cat weather.txt 20070503241737385605100.00121.3016.370 20070503241737385605100.00121.3016.370 $ ./sample.py ((2007, 5, 3), (24, 17, 37), (38, 56, 5), (100.0, 121.3, 16.37)) ((2007, 5, 3), (24, 17, 37), (38, 56, 5), (100.0, 121.3, 16.37)) $
0 コメント:
コメントを投稿