Last active
November 21, 2015 14:24
-
-
Save nutszebra/dd369ee2cd2a214bbbd3 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を入力ファイルとして実行せよ. | |
さらに,同様の処理をUNIXコマンドでも実行し,プログラムの実行結果を確認せよ. | |
Question 10: | |
10. 行数のカウント | |
行数をカウントせよ.確認にはwcコマンドを用いよ. | |
""" | |
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) | |
def countLines(path): | |
with file(path, "r") as f: | |
count = 0 | |
for line in f.readlines(): | |
count = count + 1 | |
return count | |
def wc(path): | |
cmd = "cat " + path + " | wc" | |
with file("./wc.txt", "wb") as f: | |
subprocess.call(cmd, stdout=f, shell=True) | |
with file("./wc.txt", "r") as f: | |
status = f.readline().split() | |
remove("./wc.txt") | |
return status | |
def remove(path): | |
if os.path.exists(path): | |
os.remove(path) | |
#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 | |
print("number of lines by python: " + str(countLines("./hightemp.txt"))) | |
print("number of lines by wc: " + str(wc("./hightemp.txt")[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment