2020年2月29日土曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 10(Reading and Writing Files)、Exercise 4の解答を求めてみる。

コード

#!/usr/bin/env python3
import pprint
from typing import TextIO

print('4.')


def skip_header(reader: TextIO) -> str:
    reader.readline()
    line = reader.readline()
    while line.startswith('#'):
        line = reader.readline()
    return line


def process_file(reader: TextIO) -> None:
    line = skip_header(reader).strip()
    print(line)
    print(reader.read())


if __name__ == "__main__":
    with open('hopedale.txt') as input_file:
        process_file(input_file)

入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))

% cat hopedale.txt
Coloured fox fur production, HOPEDALE, Labrador, 1834-1842
#Source: C. Elton (1942) "Voles, Mice and Lemmings", Oxford Univ. Press
#Table 17, p.265--266
      22   
      29   
       2   
      16   
      12   
      35   
       8   
      83   
     166   
% ./sample4.py    
4.
22
      29   
       2   
      16   
      12   
      35   
       8   
      83   
     166   

% 

0 コメント:

コメントを投稿