Parsing sizes

P

pinkfloydhomer

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
 
M

Magnus Lycka

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top