# Perl support Hexadecimal (start with 0x) # 0xff # # Binay (starts with 0b) # 0b01001011001 # # and Octal (starts with 0 and all numbers) notations # 0367 # # From perldata: # You are allowed to use underscores (underbars) in numeric literals between # digits for legibility. You could, for example, group binary digits by threes # (as for a Unix-style mode argument such as 0b110_100_100) or by fours (to # represent nibbles, as in 0b1010_0110) or in other groups. from string import atoi, atof def convert(perlstr): perlstr = perlstr.replace('_', '') if perlstr[:2] == '0x': num = atoi(perlstr[2:], 16) elif perlstr[:2] == '0b': num = atoi(perlstr[2:], 2) elif perlstr[0] == '0' and len(perlstr) > 1: num = atoi(perlstr[1:], 8) else: splitted = perlstr.lower().split('e') perlstr = splitted[0] if '.' in perlstr: num = atof(perlstr) else: num = atoi(perlstr) if len(splitted) == 2: return num * (10 ** atoi(splitted[1])) return num if __name__ == '__main__': print '42_000_000 ->', convert('42_000_000') print '0xdead_beef ->', convert('0xdead_beef') print '0b111_010_110 ->', convert('0b111_010_110') print '0777 ->', convert('0777') print '3.14e100 ->', convert('3.14e100')