Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

q13, q14, q15, q16 を解答 #31

Open
wants to merge 9 commits into
base: inaoka
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions chap02/q15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# coding:utf-8

import sys
from collections import deque

args = sys.argv

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これくらい単純なプログラムはsys.argvでも良いと思います。
高機能なargparseが使えるようになると、言語処理では結構できることが広がるかと

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

外部ライブラリを何かライブラリを使うなら clickってやつがオススメです.
http://click.pocoo.org/5/

n = int(args[1])
path = args[2]

with open(path, "r") as fin:
print(*deque(fin, n), sep="", end="")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このdequeの使い方いいね:+1:

# python q15.py 4 col1.txt
# tail -n 4 col1.txt