如何使用 python 处理将值添加到具有现有值的 txt 文件的第一行
How to handle adding a value to first line of txt file that has an existing value using python
如需文件处理方面的帮助,请:
所以,我是 Python 的新手,我决定直接写一个小程序。基本上,想法是有一个 .txt 文件,在一行中保持 运行 平衡,这是从用户输入接收的。但是,我很难理解如何将用户输入写入单行的值。话虽如此,这就是我必须提供的参考:
print("\n"""" Please choose an option:
Balance:
1. View
2. Update
-------------
Savings:
3. View
4. Update""""\n")
number = int(input(" Enter a number: "))
if number == 1:
view = open("current_balance.txt", "r")
print("\n", "Balance: " + view.read())
elif number == 2:
new_entry = input("\n" + " Enter figure: ")
update_balance = open("current_balance.txt", "a+")
update_balance.write(new_entry)
update_balance = open("current_balance.txt", "r")
print(update_balance.read())
update_balance.close()
elif number == 3:
print("test")
elif number == 4:
print("test")
如果问的不是太多,一些一般性的指导会很好。谢谢!
如果我对你的问题的理解正确,你想在新的一行上写下新的余额,同时跟踪旧的余额。您可以做的是以下
print("\n"""" Please choose an option:
Balance:
1. View
2. Update
-------------
Savings:
3. View
4. Update""""\n")
number = int(input(" Enter a number: "))
if number == 1:
view = open("current_balance.txt", "r")
print("\n", "Balance: " + view.read())
elif number == 2:
new_entry = input("\n" + " Enter figure: ")
update_balance = open("current_balance.txt", "a")
update_balance.write(new_entry + "\n")
update_balance = open("current_balance.txt", "r")
balances= list(map(int, update_balance.readlines()))
total_balance = sum(balances)
print(balances, "\n", total_balance)
update_balance.close()
elif number == 3:
print("test")
elif number == 4:
print("test")
据我了解
View
:应显示余额(存储为文件的第一行)
Update
:应将用户输入值与现有余额值相加,并将这两者的总和写在第一行。
这是您需要的代码。
print("\n"""" Please choose an option:
Balance:
1. View
2. Update
-------------
Savings:
3. View
4. Update""""\n")
number = int(input(" Enter a number: "))
if number == 1:
view = open("current_balance.txt", "r")
print("\n", "Balance: " + view.read())
view.close()
elif number == 2:
new_entry = input("\n" + " Enter figure: ")
update_balance = open("current_balance.txt", "r")
# The first line of the file - Existing Balance
existing_val = update_balance.read()
update_balance = open("current_balance.txt", "w")
# If the first line is not empty, Add User's input value and existing value and write the sum to the file.
if existing_val != "":
value = int(new_entry) + int(existing_val)
update_balance.write(str(value) + '\n')
else:
update_balance.write(new_entry)
update_balance = open("current_balance.txt", "r")
print(update_balance.read())
update_balance.close()
elif number == 3:
print("test")
elif number == 4:
print("test")
如需文件处理方面的帮助,请:
所以,我是 Python 的新手,我决定直接写一个小程序。基本上,想法是有一个 .txt 文件,在一行中保持 运行 平衡,这是从用户输入接收的。但是,我很难理解如何将用户输入写入单行的值。话虽如此,这就是我必须提供的参考:
print("\n"""" Please choose an option:
Balance:
1. View
2. Update
-------------
Savings:
3. View
4. Update""""\n")
number = int(input(" Enter a number: "))
if number == 1:
view = open("current_balance.txt", "r")
print("\n", "Balance: " + view.read())
elif number == 2:
new_entry = input("\n" + " Enter figure: ")
update_balance = open("current_balance.txt", "a+")
update_balance.write(new_entry)
update_balance = open("current_balance.txt", "r")
print(update_balance.read())
update_balance.close()
elif number == 3:
print("test")
elif number == 4:
print("test")
如果问的不是太多,一些一般性的指导会很好。谢谢!
如果我对你的问题的理解正确,你想在新的一行上写下新的余额,同时跟踪旧的余额。您可以做的是以下
print("\n"""" Please choose an option:
Balance:
1. View
2. Update
-------------
Savings:
3. View
4. Update""""\n")
number = int(input(" Enter a number: "))
if number == 1:
view = open("current_balance.txt", "r")
print("\n", "Balance: " + view.read())
elif number == 2:
new_entry = input("\n" + " Enter figure: ")
update_balance = open("current_balance.txt", "a")
update_balance.write(new_entry + "\n")
update_balance = open("current_balance.txt", "r")
balances= list(map(int, update_balance.readlines()))
total_balance = sum(balances)
print(balances, "\n", total_balance)
update_balance.close()
elif number == 3:
print("test")
elif number == 4:
print("test")
据我了解
View
:应显示余额(存储为文件的第一行)Update
:应将用户输入值与现有余额值相加,并将这两者的总和写在第一行。
这是您需要的代码。
print("\n"""" Please choose an option:
Balance:
1. View
2. Update
-------------
Savings:
3. View
4. Update""""\n")
number = int(input(" Enter a number: "))
if number == 1:
view = open("current_balance.txt", "r")
print("\n", "Balance: " + view.read())
view.close()
elif number == 2:
new_entry = input("\n" + " Enter figure: ")
update_balance = open("current_balance.txt", "r")
# The first line of the file - Existing Balance
existing_val = update_balance.read()
update_balance = open("current_balance.txt", "w")
# If the first line is not empty, Add User's input value and existing value and write the sum to the file.
if existing_val != "":
value = int(new_entry) + int(existing_val)
update_balance.write(str(value) + '\n')
else:
update_balance.write(new_entry)
update_balance = open("current_balance.txt", "r")
print(update_balance.read())
update_balance.close()
elif number == 3:
print("test")
elif number == 4:
print("test")