我真的是 python 新手,正在寻找一些帮助。我有一个包含当前数据的文本文件:
Tue Jun 25 **15** 336 0 0 0 0 0
Tue Jun 25 **04** 12682 0 0 0 0 0
Tue Jun 25 **05** 12636 0 0 0 0 0
Tue Jun 25 **06** 12450 0 0 0 0 0
Tue Jun 25 **07** 12640 0 0 0 0 0
我想遍历每一行并检查是否大于 12。如果大于 12,我想从中减去 12,然后用新数字写回。
以下是我到目前为止的代码:
infile = open("filelocation", "a+") #open the file with the data above and append / open it
def fun (line, infile): # define a function to to go to position 12 - 14 (which is where the date in bod is) and set it to an integer
t = infile[12:14]
p = int(t)
if p > 12: # here is the logic to see if it is greater then 12 to subtract 12 and attempt to write back to the file.
p = p - 12
k = str(p)
infile.write(k)
else:
print p # probably not needed but i had it here for testing
return
# I was having an issue with going to the next line and found this code.
for line in infile:
print line, infile
line = fun(line, infile.next())
break
infile.close()
主要问题是它没有遍历每一行或进行更新。甚至可能有更好的方法来做我想做的事情,只是还没有知识或了解某些功能的能力。对此的任何帮助将不胜感激!