以前sedで行った内容について、Pythonで書き換える必要があったので一応備忘で残しとく。 特に難しい処理をするわけでもなく、文字列を読み込んで1文字づつ大文字・小文字・数字に合わせて処理をしてやればいいだけだ。 今回は、各文字列についてgrepの正規表現に書き換えてやるようにする。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
upper = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
lower = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
digit = ['0','1','2','3','4','5','6','7','8','9']
def charReplace(string):
str_list = list(string)
rtn_list = []
for s in str_list:
if s in upper:
rtn_list.append("[[:upper:]]")
elif s in lower:
rtn_list.append("[[:lower:]]")
elif s in digit:
rtn_list.append("[[:digit:]]")
else:
rtn_list.append("[[:punct:]]")
return ''.join(rtn_list)
lines = sys.stdin.readlines()
for line in lines:
line = line.rstrip("\n")
print(line + ": " + charReplace(line))
blacknon@BS-PUB-DEVELOP:~$ cat 1c_test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
upper = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
lower = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
digit = ['0','1','2','3','4','5','6','7','8','9']
def charReplace(string):
str_list = list(string)
rtn_list = []
for s in str_list:
if s in upper:
rtn_list.append("[[:upper:]]")
elif s in lower:
rtn_list.append("[[:lower:]]")
elif s in digit:
rtn_list.append("[[:digit:]]")
else:
rtn_list.append("[[:punct:]]")
return ''.join(rtn_list)
lines = sys.stdin.readlines()
for line in lines:
line = line.rstrip("\n")
print(line + ": " + charReplace(line))
blacknon@BS-PUB-DEVELOP:~$
blacknon@BS-PUB-DEVELOP:~$ cat test.list
Strings!?999
Strings!@123
sTring888!@#
$triNgS123@@
Abcdefg@$098
blacknon@BS-PUB-DEVELOP:~$
blacknon@BS-PUB-DEVELOP:~$ cat test.list | python ./1c_test.py
Strings!?999: [[:upper:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:punct:]][[:punct:]][[:digit:]][[:digit:]][[:digit:]]
Strings!@123: [[:upper:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:punct:]][[:punct:]][[:digit:]][[:digit:]][[:digit:]]
sTring888!@#: [[:lower:]][[:upper:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:digit:]][[:digit:]][[:digit:]][[:punct:]][[:punct:]][[:punct:]]
$triNgS123@@: [[:punct:]][[:lower:]][[:lower:]][[:lower:]][[:upper:]][[:lower:]][[:upper:]][[:digit:]][[:digit:]][[:digit:]][[:punct:]][[:punct:]]
Abcdefg@$098: [[:upper:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:lower:]][[:punct:]][[:punct:]][[:digit:]][[:digit:]][[:digit:]]