Skip to content

Instantly share code, notes, and snippets.

@nutszebra
Last active November 21, 2015 14:59
Show Gist options
  • Save nutszebra/f13186f8c641d2fb89a1 to your computer and use it in GitHub Desktop.
Save nutszebra/f13186f8c641d2fb89a1 to your computer and use it in GitHub Desktop.
#!/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 12:
12. 1列目をcol1.txtに,2列目をcol2.txtに保存
各行の1列目だけを抜き出したものをcol1.txtに,2列目だけを抜き出したものをcol2.txtとしてファイルに保存せよ.
確認にはcutコマンドを用いよ.
"""
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 readFile(path):
answer = ""
with file(path, "r") as f:
for line in f.readlines():
answer = answer + line
return answer
def remove(path):
if os.path.exists(path):
os.remove(path)
def cut(path, column):
answer = ""
cmd = "cut -f " + str(column) + " " + path
with file("./cut.txt", "wb") as f:
subprocess.call(cmd, stdout=f, shell=True)
with file("./cut.txt", "r") as f:
for line in f.readlines():
answer = answer + line
remove("./cut.txt")
return answer
def saveFile(array, path):
with file(path, "wb") as f:
for ele in array:
f.write(ele + "\n")
#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 = readFile("./hightemp.txt")
col1 = [val for line in content.split("\n") for val, index in zip(line.split("\t"), range(len(line))) if index == 0]
col2 = [val for line in content.split("\n") for val, index in zip(line.split("\t"), range(len(line))) if index == 1]
saveFile(col1, "./col1.txt")
saveFile(col2, "./col2.txt")
col1Saved = readFile("./col1.txt")
col2Saved = readFile("./col2.txt")
print("original file: \n" + content)
print("col1: \n" + "\n".join(col1) + "\n")
print("col2: \n" + "\n".join(col2) + "\n")
print("saved col1: \n" + col1Saved)
print("saved col2: \n" + col2Saved)
print("cut column1: \n" + cut("./hightemp.txt",1))
print("cut column2: \n" + cut("./hightemp.txt",2))
remove("./col1.txt")
remove("./col2.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment