尝试将 GPS 时间转换为 UTC 日期和时间。输入是空格分隔的 x y 秒流,输出是 x y z 日期时间流
其设计用于处理激光雷达数据,输入可能来自“las2txt -parse xyt ...”等命令。
import datetime
import fileinput
#number of seconds between the start of unix time (Jan 1, 1970) and gps time (Jan 6, 1980)
offset = 315964800
def countleaps(gpsTime):
"""Count number of leap seconds that have passed."""
# a tuple of the gps times where leap seconds were added
leaps = (
46828800, 78364801, 109900802, 173059203, 252028804,
315187205, 346723206, 393984007, 425520008, 457056009,
504489610, 551750411, 599184012, 820108813, 914803214,
1025136015
)
nleaps = 0
for leap in leaps:
if gpsTime >= leap:
nleaps += 1
return nleaps
if __name__ == '__main__':
for line in fileinput.input(E:\LiDAR\Staging\GW_Workspace\Beaverhead_Reports\BH_0001.txt):
xvalue, yvalue, zvalue, timevalue=line.split(' ')[:4]
gpstime=float(timevalue)
gpstime += 1e9; # unadjusted GPS time
unixtime = gpstime + offset - countleaps(gpstime);
datetimestr = datetime.datetime.fromtimestamp(unixtime).strftime('%Y-%m-%d %H:%M:%S')
print xvalue, yvalue, zvalue, datetimestr
错误:
Traceback (most recent call last):
File "C:\Users\cwc051\PycharmProjects\sandbox\.venv\Sanbox_script.py", line 29, in <module>
xvalue, yvalue, zvalue, timevalue=line.split(' ') [:4]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 4, got 3)
您的问题在这里:
timevalue=line.split(' ')[:4]
您告诉 python“使用分割操作产生的数组中的前四个元素为这四个变量赋值”,但根据您收到的错误内容,至少一个
line
只有两个其中有空格,因此数组只有三个元素,因此没有第四个元素可分配给 timevalue
变量。