Pythonで[1k](キロ)とか[1G](ギガ)とかを整数の値にする
[1k](キロ)とか[1G](ギガ)とかを整数の値に直す。
import re def StrSizeToByteSize(str): r = re.compile(r"(?P<value>^([1-9]\d*|0)(\.\d+)?)(?P<fig>[kKmMgGtTpP]?)") res = r.search(str) if res != None: mag = 1 if res.group("fig") == "k" or res.group("fig") == "K": mag = 1024 if res.group("fig") == "m" or res.group("fig") == "M": mag = 1024 * 1024 if res.group("fig") == "g" or res.group("fig") == "G": mag = 1024 * 1024 * 1024 if res.group("fig") == "t" or res.group("fig") == "T": mag = 1024 * 1024 * 1024 * 1024 if res.group("fig") == "p" or res.group("fig") == "P": mag = 1024 * 1024 * 1024 * 1024 * 1024 return int(float(res.group("value")) * mag) return None
一応[P](ペタ)まで。
少数以下切り捨て。
「1kg」とかは一番最初の文字優先
1kg -> 1024
1gk -> 1048576