なぜか下書きで放置されていたので。
parse を用いる
pip install parse
にてインストールした後に、
from parse import parse parse("{:d}{:S}", "500円").fixed, parse("{:d}{:S}", "50.0円").fixed, parse("{:f}{:S}", "50.0円").fixed
結果
((500, '円'), (50, '.0円'), (50.0, '円'))
str.isdecimal(), str.isdigit(), str.isnumeric() を用いる
''.join(c for c in "500円" if c.isdigit()), ''.join(c for c in "50.0円" if c.isdigit()), ''.join(c for c in "50.0万円" if c.isdecimal())
結果
('500', '500', '500')
str.isdigit()
などは .
を数値判定できないので
''.join(c for c in "50.0円" if c.isdigit())
の出力も 500
になる点に注意。
以下ドキュメントのstr.isdigit()
の項目。
正規表現を用いる
import re re.sub(r'\D', '', "500円"), re.sub(r'\D', '', "50.0円")
結果
('500', '500')