该代码仅打印文本文件的第一行
The code only prints the first line of the text file
我认为问题出在嵌套函数中 Dis_Details()
我想做的是打印文本文件中的每一行,但是当我 运行 代码时,它只 运行s 文本文件中的 1 索引并且不会超出它。(如果没有在文本文件的下一行它应该停止但它不能正常工作)
def Customer_Details():
Name = input('Enter the customer name: ')
Phone = input('Enter the customer phone number: ')
with open('Customer Details.txt', '+a')as Details:
Details.write('\n'+Name)
Details.write(', '+Phone)
Details.close()
print('Data Saved... Returning to Main menu')
time.sleep(1)
Main()
def Recall_Details():
def Dis_Details():
Empty = False
num = 0
with open('Customer Details.txt','r+')as details:
for line in details:
Info = details.readlines()
if not line:
break
else:
print('Customer '+str(num)+': '+Info[num])
num += 1
details.close()
Choice = input('would you like to check customer Details?(y): ')
if Choice == 'y':
Dis_Details()
```
Unfortunately i have lost the rest of this code and dont know how to make it better
with open('Customer Details.txt','r+') as details:
for num, line in enumerate(details.read().split("\n")):
print('Customer ' + str(num) + ': '+ line)
也许是这样的?
您可能想要获取一组读取行然后使用它,在这种情况下您只需要:
def dis_details(path):
lines = []
with open(path) as file:
lines = file.read().splitlines()
for i in range(len(lines)):
print(f"Costumer {i}: {lines[i]}")
我认为问题出在嵌套函数中 Dis_Details() 我想做的是打印文本文件中的每一行,但是当我 运行 代码时,它只 运行s 文本文件中的 1 索引并且不会超出它。(如果没有在文本文件的下一行它应该停止但它不能正常工作)
def Customer_Details():
Name = input('Enter the customer name: ')
Phone = input('Enter the customer phone number: ')
with open('Customer Details.txt', '+a')as Details:
Details.write('\n'+Name)
Details.write(', '+Phone)
Details.close()
print('Data Saved... Returning to Main menu')
time.sleep(1)
Main()
def Recall_Details():
def Dis_Details():
Empty = False
num = 0
with open('Customer Details.txt','r+')as details:
for line in details:
Info = details.readlines()
if not line:
break
else:
print('Customer '+str(num)+': '+Info[num])
num += 1
details.close()
Choice = input('would you like to check customer Details?(y): ')
if Choice == 'y':
Dis_Details()
```
Unfortunately i have lost the rest of this code and dont know how to make it better
with open('Customer Details.txt','r+') as details:
for num, line in enumerate(details.read().split("\n")):
print('Customer ' + str(num) + ': '+ line)
也许是这样的?
您可能想要获取一组读取行然后使用它,在这种情况下您只需要:
def dis_details(path):
lines = []
with open(path) as file:
lines = file.read().splitlines()
for i in range(len(lines)):
print(f"Costumer {i}: {lines[i]}")