gpt4 book ai didi

最后一行的 Python 特定项目

转载 作者:行者123 更新时间:2023-12-04 19:21:38 27 4
gpt4 key购买 nike

我已经为过去几天遇到的许多问题找到了解决方案,但还需要一个解决方案。

我将使用 Python 脚本从 TXT 文件中读取、分隔项目、添加时间并另存为另一个 TXT 文件。

问题是 Python 一直在读取第一行,并且只使用 TXT 文件中的第一行。

我的带有数据的 txt 文件如下所示:

T:  55% 24.50   12% 90% N
T: 55% 25.50 12% 90% N
T: 55% 26.50 12% 90% N
T: 55% 27.50 12% 90% N

我的新 txt 文件看起来像:
2013-05-10 21:42:13 24.50
2013-05-10 21:42:14 24.50

虽然它应该看起来像:
2013-05-10 21:42:13 24.50
2013-05-10 21:42:14 25.50

你能帮我编辑我的脚本以将 Python 脚本设置为只读来自 txt 文件的最后一行、分隔项目、添加时间并将它们保存到新的 txt 文件中吗?

我的脚本看起来像:
#!/usr/bin/python

import time

buffer = bytes()
f = open("arduino.txt")
while buffer.count('T:') < 2:
buffer += f.read(30)
f.close();
# Now we have at least one complete datum. Isolate it.
start = buffer.index('T:')
end = buffer.index('T:', start+1)
items = buffer[start:end].strip().split()

print time.strftime("%Y-%m-%d %H:%M:%S"), items[2]

此致。

最佳答案

您可以像这样阅读最后一行:

fh = open("arduino.txt")
for line in fh:
pass
last = line

现在,
#You might want to split based on specific delimiters, but I am using the default
split_line = last.split() #T: 55% 27.50 12% 90% N

现在,打开另一个文件,并保存内容
#!/usr/bin/python

import time

f = open("arduino.txt")
while line in f:
pass
f.close();
#line is the last line now

items = line.strip().split()

print time.strftime("%Y-%m-%d %H:%M:%S"), items[2]
#Write to the new file here.

关于最后一行的 Python 特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com