[英]Python/list/file/loop/append
I have to open a file and loop through the list.我必须打开一个文件并循环遍历列表。 Then I have to print the results and append/write certain lines to the same file.
然后我必须打印结果并将某些行附加/写入同一文件。 I want to be able to run the code multiple times, but I do not want to append certain lines multiple times and I do not want to read appended lines.
我希望能够多次运行代码,但我不想 append 某些行多次,我也不想阅读附加的行。 The question is - how to append/write only once and how to skip reading appended lines?
问题是 - 如何仅附加/写入一次以及如何跳过读取附加行? Here is the code:
这是代码:
kitty = 500
requests = []
file = open("loan_requests.txt", "r+")
requests = file.readlines()
for item in requests:
flag = bool(int(item))
if flag == False:
break
if int(item) <= kitty and kitty > 0:
kitty = kitty - int(item)
loan = int(item)
print(loan, "- Paid!")
file.write("Request of {} paid in full.\n".format(loan))
elif int(item) > kitty and kitty > 0:
kitty = kitty - int(item)
loan = int(item) + kitty
print(int(item), "Request cannot be processed in full (Insufficient funds available). Amount paid:", loan)
file.write("Request of {} could not be paid in full.Partial payment of {} made.\n".format(item, loan))
elif int(item) > kitty and kitty <= 0:
print("Request of", int(item), "is UNPAID!")
file.write("Outstanding Request:{}\n".format(item))
file.close()
Tried with seek(); tell().
Before writing to the file, check if the file already contains that line.在写入文件之前,检查文件是否已经包含该行。
kitty = 500
requests = []
file = open("loan_requests.txt", "r+")
requests = file.readlines()
def write_new(file, line, requests):
if line not in requests:
file.write(line)
for item in requests:
if int(item) < kitty and kitty > 0:
kitty = kitty - int(item)
loan = int(item)
write_new(file, "Request of {} paid in full.\n".format(loan), requests)
print(loan, "- Paid!")
elif int(item) > kitty and kitty > 0:
kitty = kitty - int(item)
loan = int(item) + kitty
write_new(file, "Request of {} could not be paid in full.Partial payment of {} made.\n".format(item, loan), requests)
print(int(item), "request cannot be processed in full (Insufficient funds available). Amount paid:", loan)
elif int(item) > kitty and kitty <= 0:
write_new(file, "Outstanding Request:{}\n".format(item), requests)
print("Request of", int(item), "is UNPAID!")
file.close()
Converting what Barman suggests, let's start with helper function that will write or not depends on some flag:转换 Barman 的建议,让我们从 helper function 开始,它是否会写入取决于一些标志:
SHOULD_WRITE = True
def maybe_write(file, message):
if SHOULD_WRITE:
file.write(message)
Now use that function instead of file.write
:现在使用 function 而不是
file.write
:
# file.write("Request of {} paid in full.".format(loan))
maybe_write(file, f"Request of {loan} paid in full.")
Next time, change value of SHOULD_WRITE = False
下次,更改
SHOULD_WRITE = False
的值
BONUS : Do you know python has such fancy syntax?奖励:你知道python有这么花哨的语法吗?
if 0 < kitty < int(item):
...
kitty = 500
requests = []
file = open('loan_requests.txt')
for line in file:
if line.strip().isdecimal():
requests.append(int(line))
file.close()
with open('loan_requests.txt', 'a') as file:
for request in requests:
if request <= kitty:
file.write('\nRequest of ' + str(request) + ' paid in full.')
print(str(request) + ' - Paid!')
kitty -= request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.