我有文本文件,并且此文件有许多行具有相同的格式。
$ GPRMC, 073305.00 ,A,1349.172466,N,10030.889330,E,1.8,159.3,030419,1.1,W,A * 29 $ GPRMC, 073306.00 ,A,1349.172245,N,10030.889379,E,1.1,159.2,030419,1.1,W,A * 28 $ GPRMC, 073307.00 ,A,1349.172447,N,10030.889320,E,0.0,159.5,030419,1.1,W,A * 26 $ GPRMC, 073308.00 ,A,1349.172618,N,10030.889166,E,0.9,159.5,030419,1.1,W,A * 28 $ GPRMC, 073309.00 ,A,1349.172773,N,9930.888557,E,3.1,284.6,030419,1.1,W,A * 29 $ GPRMC, 073310.00 ,A,1349.173136,N,9830.887477,E,3.8,284.6,030419,1.1,W,A * 22 $ GPRMC, 073311.00 ,A,1349.173632,N,10130.886056,E,5.4,282.7,030419,1.1,W,A * 2B $ GPRMC, 073312.00 ,A,1349.174155,N,9530.884440,E,5.0,286.9,030419,1.1,W,A * 26
我想将时间(粗体 hhmmss.ms )更改为 +7小时,然后编辑并删除一些文本。
我认为这样的结果。
时间 14 3305.00 Lat 1349.172466 N龙10030.889330 E
时间 14 3306.00纬度1349.172245 N朗10030.889379 E
时间 14 3307.00 Lat 1349.172447 N龙10030.889320 E
时间 14 3308.00纬度1349.172618 N朗10030.889166 E
时间 14 3309.00 Lat 1349.172773 N朗9930.888557 E
时间 14 3310.00 Lat 1349.173136 N Lon 9830.887477 E
时间 14 3311.00纬度1349.173632朗10130.886056 E
时间 14 3312.00纬度1349.174155 N龙9530.884440 E
我开始从Google改编此代码以删除文本。
# Open the file as read
f = open("/home/pi/GPS.TXT/gpsdata.2019-05-09_21:54.txt", "r+")
# Create an array to hold write data
new_file = []
# Loop the file line by line
for line in f:
# Split A,B on , and use first position [0], aka A, then add to the new array
only_a = line.split(",")
# Add
new_file.append(only_a[1])
new_file.append(only_a[3])
new_file.append(only_a[4])
new_file.append(only_a[5])
new_file.append(only_a[6])
# Open the file as Write, loop the new array and write with a newline
with open("/home/pi/GPS.TXT/gpsdata.2019-05-09_21:54.txt", "w+") as f:
for i in new_file:
f.write(i+"\n")
但它会在每个文本中添加新行。这样。
145510.00
1357.157766
N
10036.988721
E
145511.00
1357.157766
N
10036.988722
E
145512.00
1357.157766
N
10036.988721
E
145513.00
1357.157766
N
10036.988721
E
我如何更改时间和编辑此文本。 我不知道该怎么办。我是python的新手。 我应该在哪里开始编写此代码?更改时间,删除文本或编辑文本? 谢谢
答案 0 :(得分:0)
您写入文件的方式:
Action.OpenUrl
从for i in new_file:
f.write(i+"\n")
获取当前索引并将其作为新行写入文件,因此会发生格式化。
要解决此问题,您需要在写入之前对行进行格式化,因此将new_data
块更改为以下内容:
# Add
关于偏移到7个小时,该时间如何编码?我猜想它是# don't add column by column to a dict; format a string to your liking and add it instead.
new_file.append("{0} {1} {2} {3} {4}".format(only_a[1], only_a[3], only_a[4], only_a[5], only_a[6]
格式(小时,分钟,秒,英里),因此您可以使用%H%M%S.%f
和datetime
做这样的事情:
timedelta
执行此操作后,只需将我的代码中的# import these
from datetime import datetime, timedelta
# get time from your data and convert it to datetime object
current_dt = datetime.strptime(only_a[1], '%H%M%S.%f')
# add 7 hours
future_dt = current_dt + timedelta(hours=7)
替换为only_a1[1]
,它就可以正常工作。
编辑:完整的解决方案,因为您在解决它时遇到问题
future_dt.strftime("%H%M%S.%f")
答案 1 :(得分:0)
现在我尝试这个。
from datetime import datetime, timedelta
# Open the file as read
f = open("/home/pi/GPS.TXT/gpsdata.2019-05-09_21:54.txt", "r+")
# Create an array to hold write data
new_file = []
# Loop the file line by line
for line in f:
# Split A,B on , and use first position [0], aka A, then add to the new array
only_a = line.split(",")
current_dt = datetime.strptime(only_a[1],'%H%M%S.%f')
future_dt = current_dt + timedelta(hours=7)
# Add
new_file.append("{0} {1} {2} {3} {4}".format(future_dt, only_a[3], only_a[4], only_a[5], only_a[6]))
# Open the file as Write, loop the new array and write with a newline
with open("/home/pi/text1.txt", "w+") as f:
for i in new_file:
f.write(i+"\n")
,输出为
1900-01-01 21:56:48 1357.157782 N 10036.988713 E
最后一行(我会循环尝试) 它在时间之前添加日期。但是日期不是当前日期。 我该怎么办?