|
コンテンツ
|
|
はじめに
- このサイトを立ち上げるに当って一番の問題はいかにラクしてコ...
|
|
お題:7 entab.py
- お題6の逆、スペースをタブに展開して、出力するプログラムを...
|
|
アーティクル
|
|
|
|
|
|
|
|
|
お題:7 entab.py
お題6の逆、スペースをタブに展開して、出力するプログラムを作成してください。タブ幅はオプションで変更できるようにします(タブ幅8桁)。
ishimoto at gembook.orgさんのお答え
import sys, re
re_spc = re.compile("([^ ]*)( +)$")
def entab(s, tablen=8):
ret = []
for col in range(0, len(s), tablen):
chars = s[col:col+tablen]
m = re_spc.match(chars)
if m:
ret.append(m.group(1))
ret.append("\t" * ((len(m.group(2))-1) / tablen+1))
else:
ret.append(chars)
return "".join(ret)
for l in sys.stdin.readlines():
print entab(l)
|