- 2009年5月 5日 20:04
- 日記
reモジュールで文字列置換の練習。
#! /usr/bin/python
# -*- encoding: utf-8 -*-
# Global variables
strbuf = ''
# Functions
def print_header():
print "Header\n",
return
def read_input():
global strbuf
import sys
argvs = sys.argv
argc = len(argvs)
print argvs
if (argc > 1):
print "reading ",argvs[1]
f = open(argvs[1])
line = f.readline() # 1行読み込む(改行文字も含まれる)
while line:
# print line
strbuf = strbuf + line
line = f.readline()
f.close
else:
print "no option defined"
return
def process():
global strbuf
import re
inputstr = '(\w+)'
reobj = re.compile(inputstr)
matched = reobj.search(strbuf)
if (matched):
print ','.join(matched.group())
strbuf = reobj.sub('replaced', strbuf)
del reobj
return None
def write_output():
print strbuf
return
def print_footer():
print "Footer"
return
# Main routine
if __name__ == "__main__":
print_header()
read_input()
process()
write_output()
print_footer()
- Newer: いい天気ですね
- Older: 飲みすぎ喰いすぎ注意報
