问题描述
我正在做一个项目,它可以让我更有效地记录我的收入/支出,但我似乎无法找到如何在给定的文本文件中找到最新的列,请记住代码是一个原型,因为我大约一个小时前才开始研究它。
f = open('Money.txt','a')
while True:
while True:
OP = input("Operator: ")
if OP == "+" or OP == "-":
break
else:
print("Invalid Operator,please try again.\n\n")
while True:
try:
MA = int(input("Money amount: "))
break
except ValueError:
print("Numeric value only.\n\n")
while True:
if OP == "+":
MV = input("Where you recived the money: ")
break
elif OP == "-":
MV = input("Where you spent the money: ")
break
while True:
C = input('\n\nIs "' + OP + "/" + str(MA) + "/" + MV + '" correct: ')
if C.lower() == "yes":
break
elif C.lower() == "no":
print("Restarting...\n\n")
break
else:
print("Invalid response,try again.\n\n")
if C.lower() == "yes":
break
elif C.lower() == "no":
continue
fs = OP + " $" + str(MA) + " / " + MV
f.write('\n' + fs)
f.close()
print("Values written.")
# "abcde!mdam!dskm".split("/",2) - basic idea of the code ill use after i figure out how to get latest column from text file
我不太擅长编程,所以如果您发现改进我的代码的公然方法,请继续分享,任何帮助都将受到感谢!
解决方法
我想通了,它可能不是最有效的方法,但它有效:
fc = f.readlines()
l = -1
for line in fc:
l += 1
然后您可以使用“l”来分割您的列表并对其进行任何您需要做的事情。
注意:您需要将“f = open('Money2.txt','a')”改为“f = open('Money2.txt','r+')”