開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 3(Files and Exceptions: Dealing with errors)、SHARPEN YOUR PENCIL(p.91)を解いてみる。
SHARPEN YOUR PENCIL(p.91)
コード(BBEdit)
sample85.py
#!/usr/bin/env python3 #-*- coding: utf-8 with open('sketch.txt') as data: for each_line in data: try: role, line_spoken = each_line.split(':', 1) print(role, end='') print(' said: ', end='') print(line_spoken, end='') except: print(each_line, end='')
入出力結果(Terminal)
$ ./sample91.py Man said: Is this the right room for an argument? Other Man said: I've told you once. Man said: No you haven't! Other Man said: Yes I have. Man said: When? Other Man said: Just now. Man said: No you didn't! Other Man said: Yes I did! Man said: You didn't! Other Man said: I'm telling you, I did! Man said: You did not! Other Man said: Oh I'm sorry, is this a five minute argument, or the full half hour? Man said: Ah! (taking out his wallet and paying) Just the five minutes. Other Man said: Just the five minutes. Thank you. Other Man said: Anyway, I did. Man said: You most certainly did not! Other Man said: Now let's get one thing quite clear: I most definitely told you! Man said: Oh no you didn't! Other Man said: Oh yes I did! Man said: Oh no you didn't! Other Man said: Oh yes I did! Man said: Oh look, this isn't an argument! (pause) Other Man said: Yes it is! Man said: No it isn't! (pause) Man said: It's just contradiction! Other Man said: No it isn't! Man said: It IS! Other Man said: It is NOT! Man said: You just contradicted me! Other Man said: No I didn't! Man said: You DID! Other Man said: No no no! Man said: You did just then! Other Man said: Nonsense! Man said: (exasperated) Oh, this is futile!! (pause) Other Man said: No it isn't! Man said: Yes it is! $
0 コメント:
コメントを投稿