Python:读取数字格式时遇到问题 [英] Python: trouble reading number format
问题描述
我正在读取一个文件,其中数字被列为 -4.8416932597054D-04,其中 D 是科学记数法.我以前从未见过这种符号.Python 如何将 -4.8416932597054*D*-04 读取为 -4.8416932597054*E*-04 我看过其他问题,但没有阅读任何解决此问题的内容?(或者我有但没有认出它.)谢谢.
I am reading from a file where numbers are listed as -4.8416932597054D-04, where D is scientific notation. I have never seen this notation before. How can Python read -4.8416932597054*D*-04 as -4.8416932597054*E*-04 I've looked at other question and haven't read anything that addresses this? (Or I have and didn't recognize it.) Thanks.
推荐答案
def to_float(s):
return float(s.lower().replace('d', 'e'))
你能举一个输入字符串的例子,它仍然给你同样的错误"吗?因为 to_float('-4.8416932597054D-04')
非常适合我,返回 -0.00048416932597054
.
Can you give an example of an input string that still gives you 'the same error'? Because to_float('-4.8416932597054D-04')
works perfectly for me, returning -0.00048416932597054
.
这篇关于Python:读取数字格式时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!