Read FORTRAN formatted numbers with Python

前端 未结 4 1506
一向
一向 2020-12-20 22:41

I have to read a data file that contains numbers formatted with (very) old FORTRAN style. A line of the file looks like this:

 4.500000+1 1.894719-3 4.600000         


        
4条回答
  •  攒了一身酷
    2020-12-20 23:43

    You could use a regular expression to insert the "E"s before passing the numbers to float.

    re.sub(r'(\d)([-+])', r'\1E\2', number)
    

提交回复
热议问题