简体   繁体   English

python pyfits,读取标头信息并在计算中使用

[英]python pyfits, reading header information and using in calculation

I am running some code with python and pyfits and I am reading out a line of information from the header. 我正在使用python和pyfits运行一些代码,并且正在从标头中读取一行信息。 I am getting the correct line but due to how it is written in the header it is printing out with colons separating the numbers I need. 我得到正确的行,但是由于它是如何在标题中编写的,因此用冒号分隔我所需的数字来打印出来。

the line I am running is print header[0].header['opp'] 我正在运行的行是print header [0] .header ['opp']

this prints 34:04:32.04 这打印34:04:32.04

I need to do a calculation where I add these numbers together, but do not know how to do this as they are separated by colons. 我需要进行计算,将这些数字加在一起,但是由于用冒号隔开,所以不知道该怎么做。

Something like this should solve your problem: 这样的事情应该可以解决您的问题:

header[0].header['opp'] = "34:04:32.04"
print (sum(float(x) for x in header[0].header['opp'].split(":")))

... which outputs: ...输出:

70.03999999999999

(EDIT) (编辑)

Or, if the values actually make up a time in hours, minutes and seconds: 或者,如果这些值实际上组成了以小时,分钟和秒为单位的时间:

s = "34:04:32.04"
ss = [float(x) for x in s.split(":")]
print (ss[0] + ss[1]/60 + ss[2]/3600)

... which outputs the value in hours: ...以小时为单位输出值:

34.07556666666667

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM