Python 如何在文本文件中更改时间和编辑文本

Python 如何在文本文件中更改时间和编辑文本,python,Python,我有一个文本文件,这个文件有许多相同格式的行 $GPRMC,073305.00,A、1349.17246、N、10030.889330、E、1.8159.3030419、1.1、W、A*29 $GPRMC,073306.00,A、1349.172245、N、10030.889379、E、1.1159.2030419、1.1、W、A*28 $GPRMC,073307.00,A、1349.172447、N、10030.889320、E、0.0159.5030419、1.1、W、A*26 $GPRMC

我有一个文本文件,这个文件有许多相同格式的行

$GPRMC,073305.00,A、1349.17246、N、10030.889330、E、1.8159.3030419、1.1、W、A*29 $GPRMC,073306.00,A、1349.172245、N、10030.889379、E、1.1159.2030419、1.1、W、A*28 $GPRMC,073307.00,A、1349.172447、N、10030.889320、E、0.0159.5030419、1.1、W、A*26 $GPRMC,073308.00,A,1349.172618,N,10030.889166,E,0.9159.5030419,1.1,W,A*28 $GPRMC,073309.00,A,1349.172773,N,9930.888557,E,3.1284.6030419,1.1,W,A*29 $GPRMC,073310.00,A,1349.173136,N,9830.887477,E,3.8284.6030419,1.1,W,A*22 $GPRMC,073311.00,A、1349.173632、N、10130.886056、E、5.4282.7030419、1.1、W、A*2B $GPRMC,073312.00,A、1349.174155、N、9530.884440、E、5.0286.9030419、1.1、W、A*26

我想将时间(粗体数字hhmmss.ms)更改为+7小时,并编辑和删除一些文本

结果我想是这样的

时间143305.00纬度1349.17246北纬10030.889330东经

时间143306.00纬度1349.172245北纬10030.889379东经

时间143307.00纬度1349.172447北纬10030.889320东经

时间143308.00纬度1349.172618北纬10030.889166东经

时间143309.00 Lat 1349.172773 N Lon 9930.888557 E

时间143310.00纬度1349.173136北纬9830.887477东经

时间143311.00纬度1349.173632北纬10130.886056东经

时间143312.00纬度1349.174155北纬9530.884440东经

我开始从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的新手。 在这个代码中我应该从哪里开始?更改时间、删除文本还是编辑文本?
感谢您写入文件的方式:

for i in new_file:
  f.write(i+"\n")
正在从
new\u data
中获取当前索引,并将其作为新行写入文件,这样就可以进行格式化

要解决这个问题,您需要在写入之前格式化行,因此将
#Add
块更改为如下内容:

#不要在dict中逐列添加;根据您的喜好格式化字符串并添加它。
新的_文件.append(“{0}{1}{2}{3}{4}.”格式(仅_a[1],仅_a[3],仅_a[4],仅_a[5],仅_a[6]
关于7小时的偏移,时间是如何编码的?我猜它是一种
%H%m%S.%f
格式(小时、分钟、秒、毫秒),所以你可以使用
datetime
timedelta
这样做:

#导入这些
从datetime导入datetime,timedelta
#从数据中获取时间并将其转换为datetime对象
当前\u dt=datetime.strtime(仅\u a[1],“%H%M%S.%f”)
#加7小时
未来时间=当前时间+时间增量(小时=7)
完成此操作后,只需将我的代码中的
仅替换为
未来的dt.strftime(“%H%M%S.%f”)
,它就可以工作了

编辑:完整的解决方案,因为您在解决问题时遇到了问题

从datetime导入datetime,timedelta
#以读取方式打开文件
f=打开(“/home/pi/GPS.TXT/gpsdata.2019-05-09_21:54.TXT”,“r+”)
#创建一个数组来保存写入数据
新建_文件=[]
#逐行循环文件
对于f中的行:
#拆分A、B并使用第一个位置[0],即A,然后添加到新数组中
仅_a=行。拆分(“,”)
#解析第一列的数据,这是一个特定的情况
当前\u dt=datetime.strtime(仅\u a[1],“%H%M%S.%f”)
未来时间=当前时间+时间增量(小时=7)
#添加到写入列表
新的_file.append(“{0}{1}{2}{3}{4}.”格式(future_dt.strftime('%H%M%S.%f'),仅_a[3],仅_a[4],仅_a[5],仅_a[6]))
#格式化loo完成后,现在打开输出文件并写入其中。
#以写入方式打开文件,循环新数组并使用换行符写入
将open(“/home/pi/text1.txt”,“w+”)作为f:
对于新_文件中的i:
f、 写入(i+“\n”)
现在我试试这个

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北纬10036.988713东经

最后一行(我将在循环中尝试) 它在时间之前添加日期。但日期不是当前日期。
我该怎么做?

我会试试这个。完成后我会告诉你结果。谢谢你我应该在linesplit或change Time之前或之后插入文本(Time,Lat,Lon)?你需要在格式化字符串之前完成所有文本处理,格式化字符串并在开始编写之前将其添加到列表中。我尝试新的_文件。append(“{0}{1}{2}{3}{4}”.format(仅_a[1]、仅_a[3]、仅_a[4]、仅_a[5]、仅_a[6]),但它仅以新格式(原始文件的最后一行)写入文件。为什么?您需要在循环中追加数据,如果不这样做,它将只添加最后一行。