如何让python读取整个文本文件,而不仅仅是一行?
我一直试图让我的代码搜索用户输入的产品的文本文件,但它只读取第一行,而不是我想要的整个文件 这是我的密码:如何让python读取整个文本文件,而不仅仅是一行?,python,text-files,Python,Text Files,我一直试图让我的代码搜索用户输入的产品的文本文件,但它只读取第一行,而不是我想要的整个文件 这是我的密码: order=input("Please enter the name of the product you wish to purchase\n") myfile=open("barcode.txt","r") details=myfile.readlines() #reads the file and stores it as the variable 'details
order=input("Please enter the name of the product you wish to purchase\n")
myfile=open("barcode.txt","r")
details=myfile.readlines() #reads the file and stores it as the variable 'details'
for line in details:
if order in line: #if the barcode is in the line it stores the line as 'productline'
productline=line
quantity=int(input("How much of the product do you wish to purchase?\n"))
itemsplit=productline.split(' ') #seperates into different words
price=float(itemsplit[1]) #the price is the second part of the line
total=(price)*(quantity) #this works out the price
print("Your total spent on this product is: " +'£'+str(total))
else:
break
你正在打破这个循环: 顺便说一句,我添加了一个with statent,用于以更为python的方式打开文件
order = input("Please enter the name of the product you wish to purchase\n")
with open("barcode.txt","r") as myfile:
details=myfile.readlines() #reads the file and stores it as the variable 'details'
for line in details:
if order in line: #if the barcode is in the line it stores the line as 'productline'
productline=line
quantity=int(input("How much of the product do you wish to purchase?\n"))
itemsplit=productline.split(' ') #seperates into different words
price=float(itemsplit[1]) #the price is the second part of the line
total=(price)*(quantity) #this works out the price
print("Your total spent on this product is: " +'£'+str(total))
您正在使用break过早退出循环:
for line in details:
if order in line:
# do stuff
else:
break #<-- this is exiting the loop
或者您可以完全忽略其他内容:
for line in details:
if order in line:
# do stuff
您的代码将在选中1行后中断 你有
for line in details:
if order in line:
# Does stuff
else:
break
# This breaks out of the `for line in details` loop.
因此,如果订单不在第一行,它将退出循环
你很可能正在寻找类似于
for line in details:
if order in line:
# Do stuff
break # You want to break if you found the order
else:
continue
尽管在这种情况下,不需要else:continue分支,因为如果找不到订单,您不打算执行任何操作
另外,文件自然地支持迭代,所以您不需要执行以下操作
myfile = open("barcode.txt", "r")
details = myfile.readlines()
# this line ^ can be removed, and you can just iterate over the file object itself
for line in myfile:
# do stuff
完成后,不要忘记使用myfile.close关闭文件,或者使用类似的上下文管理器
with open("barcode.txt", "r") as myfile:
for line in myfile:
# Do stuff
# Once you go out of the `with` context, the file is closed for you
要读取文件,请检查: 几点建议: 一次获取所有输入。nt like QUOTE=INTINPUTH t的多少 一次读取所有数据。 一旦你得到了所有的变量,现在开始研究逻辑。 你不需要做productline=line 如果你不需要“else”,那么就不要使用它。
检查python的Zen、python PEP8、,还有一些编程标准和风格。让你的代码看起来很酷。如果你在第一行中找不到顺序,你就会中断循环。你只得到一行的原因是因为你立即中断,用pass或suppress将其更改为else语句remove else:breakI认为continue可能更符合OP的预期虽然在这种情况下,当我运行代码时,同样是多余的,但我不断收到一条错误消息,说找不到barcode.txt。您是否在同一目录中创建了名为barcode.txt的文件?如果不是你需要创建它的话,我做的,可能只是我的电脑,它最近一直在播放。
with open("barcode.txt", "r") as myfile:
for line in myfile:
# Do stuff
# Once you go out of the `with` context, the file is closed for you
fp = open(file_name)
print fp.read() # prints all data in file name