请帮我解决一个奇怪的问题!
输入文件如下所示:
# 06/11/2017 11:45:03
2143-1445_001_1 = F 224922.53(15.04107) -144551.26 0.00 1x12.0
# 06/11/2017 11:45:21
2143-0822_001_1 = F 224940.53(15.04107) -082227.26 0.00 1x12.0
# 06/11/2017 11:45:39
2143-0159_001_1 = F 224958.54(15.04107) -015903.26 0.00 1x12.0
# 06/11/2017 11:46:01
2114-1341_001_1 = F 231840.63(15.04107) -134149.68 0.00 1x12.0
# 06/11/2017 11:46:19
2114-0718_001_1 = F 231858.63(15.04107) -071825.68 0.00 1x12.0
这是我的python代码:
with open('object.list','r') as in_file:
lines = in_file.readlines()
in_file.close()
out_file = open('newobject.list','w')
for i, line in enumerate(lines):
if i < len(lines)-2:
if '#' in line:
time_start = line[-10:].replace(':','')
time_end = lines[i+2][-10:].replace(':','')
out_file.write(line.rstrip('\n'))
else:
out_file.write(line.rstrip() + ' @' + time_start + '-' + time_end)
else:
if '#' in line:
time_start = time_end = line[-10:].replace(':','')
out_file.write(line.rstrip('\n'))
else:
out_file.write(line.rstrip() + ' @' + time_start + '-' + time_end)
out_file.close()
输出文件如下所示:
# 06/11/2017 11:45:03
2143-1445_001_1 = F 224922.53(15.04107) -144551.26 0.00 1x12.0 @114503
-114521
# 06/11/2017 11:45:21
2143-0822_001_1 = F 224940.53(15.04107) -082227.26 0.00 1x12.0 @114521
-114539
# 06/11/2017 11:45:39
2143-0159_001_1 = F 224958.54(15.04107) -015903.26 0.00 1x12.0 @114539
-114601
# 06/11/2017 11:46:01
2114-1341_001_1 = F 231840.63(15.04107) -134149.68 0.00 1x12.0 @114601
-114619
# 06/11/2017 11:46:19
2114-0718_001_1 = F 231858.63(15.04107) -071825.68 0.00 1x12.0 @114619
-114619
为什么连字符(包括后面的文字)会跳到下一行?我需要在前一行。我的代码出了什么问题?
提前致谢!