您好我在使用python修改文本行时遇到问题。我正在制作工资单应用程序,我无法使用修改方法。我不明白为什么我搜索的条目不会在新文件中删除。我将文件复制到临时文件,然后重命名临时文件,但旧条目保持不变。
def modEmp():
empFile = open("employees.txt", 'r')
empFile2= open("temp.txt",'w')
line1 = empFile.readline().rstrip("\n")
name = input("Enter in your employee name that you would like to modify\n")
while line1 != '':
line1 = line1.split(" ") #split line into list
name = line1[0] + ' ' + line1[1]
if name[0] == line1[0] and name[0] == line1[0]:
print('Enter the modified entry:\n')
list = [0] * 4
list [0] = input('Enter first name:\n')
list [1] = input('Enter last name:\n')
list [2] = input('Enter pay rate:\n')
list [3] = input('Enter hours worked:\n')
empFile2.write(list[0] + ' ' + list[1] + ' ' + list[2] + ' ' + list[3] + "\n")
else:
empFile2.write(line1 + "\n")
line1 = empFile.readline().rstrip("\n")
#Close file
empFile.close()
empFile2.close()
os.remove('employees.txt')
os.rename('temp.txt','employees.txt')
答案 0 :(得分:1)
您的代码存在一些严重问题:
name = input("Enter in your employee name that you would like to modify\n")
...
name = line1[0] + ' ' + line1[1]
在查看前会覆盖name
,所以无论输入什么都不重要。
正如Hugh Bothwell所指出的那样:
line1 = line1.split(" ") #split line into list
name = line1[0] + ' ' + line1[1]
if name[0] == line1[0] and name[0] == line1[0]:
尝试将字符串与同一字符串中的第一个字符进行比较,如果name
是单个字符,则仅为True。
相反,你想做这样的事情:我通过使用with
清理了很多额外的烦恼,处理为我们关闭文件并让内置{{1} } loop:
我还将您的列表变量重命名为for element in list:
,因为覆盖内置lst
变量会导致错误。
list
请注意,如果您有多个"账单"并且您搜索" bill",系统将提示您更改它们。此外,此功能没有中止,并且具有破坏性,因此您将丢失为这些员工存储的任何信息。在真正的工资单应用程序中,这可能是灾难性的。
此外,如果您想让它更具可重用性,而不是在列表中指定四个元素,您可以:
def modEmp():
with open("employees.txt", 'r') as empFile, open("temp.txt", "w") as empFile2:
name = input("Enter in your employee name that you would like to modify\n")
for line in empFile:
if name in line:
print('Enter the modified entry:\n')
lst = []
lst.append(input('Enter first name:\n'))
lst.append(input('Enter last name:\n'))
lst.append(input('Enter pay rate:\n'))
lst.append(input('Enter hours worked:\n'))
empFile2.write("{} {} {} {}\n".format(*lst))
else:
empFile2.write(line)
# I highly encourage making a backup, as sooner or later
# someone will mess it up
os.rename('employees.txt', 'employees.bkp')
os.rename('temp.txt','employees.txt')
这将编辑与提示一样多的输入,然后将它们作为一行分隔在文件中,尽管它不那么明显你正在做什么。
答案 1 :(得分:1)
正如评论中所提到的,您的代码存在一些问题......
在您的情况下使用with open(name) as f:
语法是good practice。
以下是我认为可以解决您问题的解决方案。
def modify():
with open("employees.txt", 'r+') as employee_file: # Opening file in read and write mode
temp = employee_file.readlines() # Get all lines
employee_file.seek(0) # Reset position in file
name = input("Enter in your employee name that you would like to "
"modify\n")
first, last = name.split(' ') # Assuming this is what you intended
for line in temp:
# There's no reason to use a list here, and avoid using object names as variables
if first in line and last in line:
print('Enter the modified entry:\n')
first_name = input('Enter first name:\n')
last_name = input('Enter last name:\n')
pay_rate = input('Enter pay rate:\n')
hours = input('Enter hours worked:\n')
line_to_write = ' '.join([first_name, last_name,
pay_rate, hours])
employee_file.write(line_to_write + "\n")
else:
employee_file.write(line + "\n")
employee_file.truncate()