Python: Converting string into decimal number

前端 未结 6 864
滥情空心
滥情空心 2020-12-03 01:00

I have a python list with strings in this format:

A1 = [\' \"29.0\" \',\' \"65.2\" \',\' \"75.2\" \']

How do I convert those strings into d

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 01:31

    If you want the result as the nearest binary floating point number use float:

    result = [float(x.strip(' "')) for x in A1]
    

    If you want the result stored exactly use Decimal instead of float:

    from decimal import Decimal
    result = [Decimal(x.strip(' "')) for x in A1]
    

提交回复
热议问题