2014年6月9日月曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 5(Comprehending data: Work that data)、CODE MAGNETS(p.149)を解いてみる。

CODE MAGNETS(p.149)

コード(BBEdit)

sample149.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

def sanitize(time_string):
    if '-' in time_string:
        spliter = '-'
    elif ':' in time_string:
        spliter = ':'
    else:
        return (time_string)
    (mins, secs) = time_string.split(spliter)
    return (mins + '.' + secs)
    
with open('james.txt') as f_james, open('sarah.txt') as f_sarah, \
     open('julie.txt') as f_julie, open('mikey.txt') as f_mikey:
    james = f_james.readline().strip().split(',')
    sarah = f_sarah.readline().strip().split(',')
    julie = f_julie.readline().strip().split(',')
    mikey = f_mikey.readline().strip().split(',')

print('james')
for time_string in james:
    print(sanitize(time_string))
print('sarah')
for time_string in sarah:
    print(sanitize(time_string))
print('julie')
for time_string in julie:
    print(sanitize(time_string))
print('mikey')
for time_string in mikey:
    print(sanitize(time_string))

入出力結果(Terminal)

$ ./sample149.py
james
2.34
3.21
2.34
2.45
3.01
2.01
2.01
3.10
2.22
sarah
2.58
2.58
2.39
2.25
2.55
2.54
2.18
2.55
2.55
julie
2.59
2.11
2.11
2.23
3.10
2.23
3.10
3.21
3.21
mikey
2.22
3.01
3.01
3.02
3.02
3.02
3.22
2.49
2.38
$

0 コメント:

コメントを投稿