Parsing sizes
Magnus Lycka
lycka at carmen.se
Wed Feb 1 09:54:18 EST 2006
pinkfloydhomer at gmail.com wrote:
> I want to be able to parse sizes in bytes in several formats, such as
> "1048576", "1024K", "1024KB", "1M" etc.
>
> Is there a module that will help me with that?
>
> /David
>
Like this?
import re
units = {'B':0, 'k':10,'M':20,'G':30,'T':40}
def parse(text):
mo = re.match(r'(\d+)(\w?)', text)
if mo:
num = int(mo.group(1))
size = mo.group(2)
if size in units:
num *= 2**int(units[size])
else:
return ValueError
return num
else:
raise ValueError
print parse('1B')
print parse('1kB')
print parse('1M')
print parse('1G')
print parse('1T')
print parse('1KB')
(Capital K isn't a proper prefix that I ever heard of.)
More information about the Python-list
mailing list