開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 8(Working with Files)、41(Name Sorter)を取り組んでみる。
41(Name Sorter)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
filename = 'names.txt'
filename1 = 'output.txt'
names = []
with open(filename) as f:
for line in f:
line = line.strip()
first, last = line.split(',')
names.append((first.strip(), last.strip()))
names.sort()
with open(filename1, 'w') as f:
print('Total of {0} names'.format(len(names)), file=f)
print('--------------------', file=f)
for first, last in names:
print('{0}, {1}'.format(first, last), file=f)
入出力結果(Terminal, IPython)
$ cat names.txt Ling, Mai Johnson, Jim Zarnecki, Sabrina Jones, Chris Jones, Aaron Swift, Geoffrey Xilong, Fong $ ./sample41.py $ cat output.txt Total of 7 names -------------------- Johnson, Jim Jones, Aaron Jones, Chris Ling, Mai Swift, Geoffrey Xilong, Fong Zarnecki, Sabrina $
0 コメント:
コメントを投稿