我如何编写代码以使用 python 编辑 txt 文件中的字符
How can i code for edit a character in a txt file with python
它在询问旧代码后保持停止 运行 并清除我的 roti canai 文件。那么我能为它做些什么呢(甚至正在更改所有代码)
def modify_roti_canai():
print("\n*-- Modify Roti Canai Menu --*\n")
file = open("roticanai", "r+")
print(file.read())
old_code = input("Enter a Item Code for Modifying (e.g. RC01): ")
if len(old_code) == 4:
new_code = input("New Food Item Code: ")
new_name = input("New Food Item Name: ")
new_price = input("New Food Item Price: ")
file.write(new_code + "," + new_name + "," + new_price + "\n")
else:
print("Item Code Not Existed, Please try again!")
file.close()
*在我的 roticanai 文件中存在很少的记录:
RC01,ROTI CANAI KOSONG,1.60
RC02,ROTI CANAI SUSU,3.90
RC03,ROTI CANAI CHEESE,4.50
RC04,ROTI CANAI PLANTA,3.90
RC05,ROTI CANAI TISSUE,4.50
s 是一个空字符串。如果您将长度(即 0)与 4 进行比较,则 if 语句的计算结果为 False。它不会将数据添加到“temp.txt”文件。它将一个空文件重命名为“roticanai”。
根据需要重命名文件名
**使用相同的文件名,不需要临时文件
def modify_roti_canai():
print("\n*-- Modify Roti Canai Menu --*\n")
with open("test.txt", "r") as file:
#Gets each line of the file as list item
data = [line.rstrip() for line in file.readlines()]
print(data)
#separates codes from data
codes= [x.split(',')[0] for x in data]
print(codes)
old_code = input("Enter a Item Code for Modifying (e.g. RC01): ")
if len(old_code) == 4:
#removes the old code line from data using the index of entered code
data.pop(codes.index(old_code))
new_code = input("New Food Item Code: ")
new_name = input("New Food Item Name: ")
new_price = input("New Food Item Price: ")
new_data = f"{new_code},{new_name},{new_price}"
#appends new data to the data list
data.append(new_data)
#Writes the data list to file
with open('test.txt','w') as file:
for e in data:
file.write(e + '\n')
print(file.read())
else:
print("Item Code Not Existed, Please try again!")
它在询问旧代码后保持停止 运行 并清除我的 roti canai 文件。那么我能为它做些什么呢(甚至正在更改所有代码)
def modify_roti_canai():
print("\n*-- Modify Roti Canai Menu --*\n")
file = open("roticanai", "r+")
print(file.read())
old_code = input("Enter a Item Code for Modifying (e.g. RC01): ")
if len(old_code) == 4:
new_code = input("New Food Item Code: ")
new_name = input("New Food Item Name: ")
new_price = input("New Food Item Price: ")
file.write(new_code + "," + new_name + "," + new_price + "\n")
else:
print("Item Code Not Existed, Please try again!")
file.close()
*在我的 roticanai 文件中存在很少的记录:
RC01,ROTI CANAI KOSONG,1.60
RC02,ROTI CANAI SUSU,3.90
RC03,ROTI CANAI CHEESE,4.50
RC04,ROTI CANAI PLANTA,3.90
RC05,ROTI CANAI TISSUE,4.50
s 是一个空字符串。如果您将长度(即 0)与 4 进行比较,则 if 语句的计算结果为 False。它不会将数据添加到“temp.txt”文件。它将一个空文件重命名为“roticanai”。
根据需要重命名文件名 **使用相同的文件名,不需要临时文件
def modify_roti_canai():
print("\n*-- Modify Roti Canai Menu --*\n")
with open("test.txt", "r") as file:
#Gets each line of the file as list item
data = [line.rstrip() for line in file.readlines()]
print(data)
#separates codes from data
codes= [x.split(',')[0] for x in data]
print(codes)
old_code = input("Enter a Item Code for Modifying (e.g. RC01): ")
if len(old_code) == 4:
#removes the old code line from data using the index of entered code
data.pop(codes.index(old_code))
new_code = input("New Food Item Code: ")
new_name = input("New Food Item Name: ")
new_price = input("New Food Item Price: ")
new_data = f"{new_code},{new_name},{new_price}"
#appends new data to the data list
data.append(new_data)
#Writes the data list to file
with open('test.txt','w') as file:
for e in data:
file.write(e + '\n')
print(file.read())
else:
print("Item Code Not Existed, Please try again!")