Chapter XV: internationalization and localization -locale: cultural localization API- All Digital

15.2.4 All Digital
In addition to generating outputs in different formats, locale can also help to parse the input module. It contains atoi () and the atof () function, may agree to convert the string to integer and floating point numeric value according to the format of the locale.

import locale

sample_data = [
    ('USA','en_US','1,234.56'),
    ('France','fr_FR','1234.56'),
    ('Spain','es_ES','1234.56'),
    ('Portugal','pt_PT','1234.56'),
    ('Poland','pl_PL','1 234,56'),
    ]

for name,loc, a in sample_data:
    locale.setlocale(locale.LC_ALL,loc)
    print('{:>10}:{:>9} => {:f}'.format(
        name,
        a,
        locale.atof(a),
        ))

Packet parser identifies the localized environment and the value of decimal separator.

Guess you like

Origin blog.csdn.net/weixin_43193719/article/details/94833311