Created
November 21, 2015 16:59
-
-
Save nutszebra/524ac287ac2f931e27be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
#Link: http://www.cl.ecei.tohoku.ac.jp/nlp100/ | |
""" | |
hightemp.txt: http://www.cl.ecei.tohoku.ac.jp/nlp100/data/hightemp.txt | |
は,日本の最高気温の記録を「都道府県」「地点」「℃」「日」のタブ区切り形式で格納したファイルである. | |
以下の処理を行うプログラムを作成し,hightemp.txtを入力ファイルとして実行せよ. | |
Question 14: | |
14. 先頭からN行を出力 | |
自然数Nをコマンドライン引数などの手段で受け取り,入力のうち先頭のN行だけを表示せよ. | |
確認にはheadコマンドを用いよ. | |
""" | |
import subprocess | |
import requests | |
import os | |
def download(url, dir, params={}): | |
dl = requests.get(url, params=params) | |
with file(dir, "wb") as f: | |
f.write(dl.content) | |
from collections import deque | |
def readText(path, howMany = "all", direction = "forward"): | |
#read all lines forwardly | |
if howMany == "all" and direction == "forward": | |
answer = "" | |
with file(path, "r") as f: | |
line = f.readline() | |
while line: | |
answer = answer + line | |
line = f.readline() | |
return answer | |
#read all lines backwardly | |
elif howMany == "all" and direction == "backward": | |
answer = "" | |
with file(path, "r") as f: | |
line = f.readline() | |
while line: | |
answer = line + answer | |
line = f.readline() | |
return answer | |
#read n-lines forwardly | |
elif direction == "forward": | |
count = 0 | |
answer = "" | |
with file(path, "r") as f: | |
line = f.readline() | |
while line: | |
if count >= int(howMany): | |
break | |
answer = answer + line | |
line = f.readline() | |
count = count + 1 | |
return answer | |
#read n-lines backwardly | |
elif direction == "backward": | |
answer = deque() | |
for i in xrange(howMany): | |
answer.append("") | |
with file(path, "r") as f: | |
line = f.readline() | |
while line: | |
answer.append(line) | |
answer.popleft() | |
line = f.readline() | |
answer.reverse() | |
answer = list(answer) | |
return "".join(answer) | |
else: | |
print("***********some options are weird***********") | |
print("options of direction: forward, backward") | |
print("howMany has to be number") | |
print("********************************************") | |
print("your direction is: " + direction) | |
print("your howMany is: " + str(howMany)) | |
return False | |
def remove(path): | |
if os.path.exists(path): | |
os.remove(path) | |
def head(path, howMany): | |
cmd = "head -n " + str(howMany) + " " + path | |
with file("./head.txt", "wb") as f: | |
subprocess.call(cmd, stdout=f, shell=True) | |
with file("./head.txt", "r") as f: | |
answer = readText("./head.txt") | |
remove("./head.txt") | |
return answer | |
#download hightemp.txt | |
download("http://www.cl.ecei.tohoku.ac.jp/nlp100/data/hightemp.txt","./hightemp.txt") if not os.path.exists("./hightemp.txt") else None | |
content = readText("./hightemp.txt") | |
lines = raw_input("how many lines would you like to see?: ") | |
readContent = readText("./hightemp.txt", howMany = int(lines)) | |
headContent = head("./hightemp.txt", howMany = lines) | |
print("original file: \n" + content) | |
print("read " + str(lines) + "lines \n" + readContent) | |
print("head " + str(lines) + "lines \n" + headContent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment