簡體   English   中英

你如何讓python讀取整個文本文件,而不僅僅是一行?

[英]How do you get python to read a whole text file, not just one line?

我一直試圖讓我的代碼搜索用戶輸入的產品的文本文件,但它只讀取第一行,而不是我想要的整個文件。

這是我的代碼:

 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

檢查第 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

你打破了循環:
(順便說一句,我添加了一個 with statent 以更pythonic的方式打開文件)

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

您想使用pass ,正如@whitefret 在評論中所說:

for line in details:
        if order in line:
            # do stuff
        else:
            pass #<--- this will continue the loop

或者你可以完全省略else

for line in details:
        if order in line:
            # do stuff

要讀取文件,請檢查: https : //docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects

fp = open(file_name)
print fp.read() # prints all data in file name

幾點建議:

  • 一次獲取所有輸入。 # nt likequantity=int(input("有多少t"
  • 一次讀取所有數據。
  • 獲得所有變量后,現在開始處理邏輯。
  • 你不需要做“productline=line”
  • 如果您不需要'else',則不要使用它。

檢查 Zen of python、Python PEP8 和一些編程標准和風格。讓你的代碼看起來很酷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM