Search code examples
stringbytefilesizemegabyte

Python recognize strings of numbers with K, M, G, or k, m, g, or KB, MB, GB, etc as filesizes for comparison or math


Using Python 3.8 or newer. I have a Pandas dataframe of numbers like below, and I need to convert each item that is not a percent into bytes or some custom format in order to compare and/or do math on them. Is there an existing library or relatively fast method to do this?

425M    410M    6.2M    99%    5.8k   51k   10%
1.0K    1.0K      0B   100%       0     0  100%
4.0K    4.0K      0B   100%       1     0  100%
7.8G    1.1G    6.1G    15%     405  1.1M    0%
94G      26G     60G    31%     68k   13M    1%


Solution

  • Well solved my own problem. Couldn't find any code out there so I wrote my own class. I'm sure there's tons of improvements that can be made. It's in my github: https://github.com/KyferEz/DataUnits