我必须从一行中提取一个数字(双精度)。我将线分成字符串并使用索引提取。例如,如果字符串
line = 'The lowest temperature ever recorded on Earth was −128.6 F in the year 1983.'
我把这行分割成数字:
line_str = line.split()
temp = line_str[8]
year = line_str[13]
由于-128.6
现在为line_str[7]
,因此以下一行将失败。
line = 'Lowest temperature ever recorded on Earth was −128.6 F in the year 1983.'
有一种通用的方法吗?
编辑:我要同时列出温度和年份。