使用D而不是E的Python科学记数法 [英] Python scientific notation using D instead of E
本文介绍了使用D而不是E的Python科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Fortran程序生成的某些结果文件使用字母D
代替E
报告双精度数字(科学计数法),例如:
Some results file produced by Fortran programs report double precision numbers (in scientific notation) using the letter D
instead of E
, for instance:
1.2345D+02
# instead of
1.2345E+02
我需要使用Python处理大量数据,我才意识到它无法读取D
表示法中的数字,例如:
I need to process huge amounts of this data using Python, and I just realized it cannot read the numbers in the D
notation, for instance:
>>> A = 1.0D+01
File "<stdin>", line 1
A = 1.0D+01
^
SyntaxError: invalid syntax
我可以更改语言环境并让Python知道D
表示E
吗?我真的不想进行全局搜索和替换!
Can I change my locale and let Python know that D
means E
? I really would not want to make a global search-and-replace!
推荐答案
在Python程序中,最简单的方法就是在解释每个条目之前添加一个步骤:
The simplest way, from your Python program, would be just to add a step before you interpret each entry:
>>> val = "1.5698D+03" # 1,569.8
>>> print float(val.replace('D', 'E'))
1569.8
这篇关于使用D而不是E的Python科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文