Skip to content

Instantly share code, notes, and snippets.

@nutszebra
Created November 20, 2015 17:10
Show Gist options
  • Save nutszebra/8e5bc3213d8b6884d89b to your computer and use it in GitHub Desktop.
Save nutszebra/8e5bc3213d8b6884d89b 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 11:
11. タブをスペースに置換
タブ1文字につきスペース1文字に置換せよ.確認にはsedコマンド,trコマンド,もしくはexpandコマンドを用いよ.
"""
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 encloseByQuotation(sentence):
if "'" in sentence:
quotation = '"'
else:
quotation = "'"
return quotation + sentence + quotation
def tr(path, before, after):
before = encloseByQuotation(before)
after = encloseByQuotation(after)
cmd = "cat " + path + " | tr " + before + " " + after + " > ./tr.txt"
subprocess.call(cmd, shell=True)
answer = ""
with file("./tr.txt", "r") as f:
for line in f.readlines():
answer = answer + line
remove("./tr.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 = readFile("./hightemp.txt")
print("original file: \n" + content)
print("Every tabs inside the file was replaced by a space: \n" + content.replace("\t"," "))
print("executed by tr: \n" + tr("./hightemp.txt","\t"," "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment