2016-04-06 16 views
-5

Kullanıcı tarafından girilen bir ürün için bir metin dosyası aramak için kodumu almaya çalışıyorum, ancak istediğim gibi tüm satırı değil, yalnızca ilk satırı okuyor. İşte Python'u sadece bir satır değil, bütün bir metin dosyasını okumak için nasıl edinirsiniz?

benim kodudur:

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 
+0

Siparişi ilk satırda bulamazsanız, döngüden koparsınız. – Selcuk

+0

Sadece bir satır almanızın sebebi, – Tgsmith61591

+1

'u "pass" ile değiştirirseniz veya – Whitefret

cevap

0

Sizin döngünün dışında kırarak edildi:

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)) 
+0

Kodu çalıştırdığım zaman, barcode.txt deyimini içeren hata iletisi bulunamadı – EmDuff

+0

Aynı dizinde 'barcode.txt' adlı bir dosya oluşturdunuz mu? (eğer bunu yaratmanız gerekmiyorsa) – Oisin

+0

Yaptım, sadece bilgisayarım olabilir, yakın zamanda çalıyor. – EmDuff

0

(Btw i daha pythonic şekilde dosyayı açmak için statent ile eklenmiştir) çok yakında döngü çıkmak için break kullanıyorsanız:

for line in details: 
     if order in line: 
      # do stuff 
     else: 
      break #<-- this is exiting the loop 

@whitefret yorumlar dedi gibi, pass kullanmak istiyorum:

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

Ya da sadece tamamen else dışarı bırakabilir: 1 satır kontrol edilir sonra

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

Sanırım "devam et", OP'nin beklediği sonuçlarla daha uyumludur (bu özel durumda eşit derecede yedek olmasına rağmen) – SpoonMeiser

3

Kodunuz patlak olacaktır.

Siparişin ilk satırda değilse

for line in details: 
    if order in line: 
     # Does stuff 
    else: 
     break 
     # This breaks out of the `for line in details` loop. 

Yani, döngü çıkar var.

Büyük olasılıkla sipariş bulunmazsa şey yürütme planlama değil olarak bu durumda, else: continue şube, gerekli olmasa da

for line in details: 
    if order in line: 
     # Do stuff 
     break # You want to break if you found the order 
    else: 
     continue 

benzer bir şey arıyor

. aşağıdakileri yapmanız gerekmez böylece gibi

kenara, dosyalar, doğal olarak yinelemeyi destekleyen

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 

İşiniz bittiğinde myfile.close() ile dosyayı kapatın ya da alternatif kullanmayı unutmayın bir dosyayı okumak için

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 
0

gibi bir bağlam yöneticisi kontrol edin: 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 

Bazı öneriler:

  • Tüm girişleri bir kerede alın. miktar = int gibi # nt (giriş ("Nasıl t kadar"
  • kerede tüm verileri okuyun.
  • tüm değişkenleri, şimdi mantık üzerinde çalışmaya başlar. Eğer "ProductLine yapmak gerekmez
  • aldığımda = Eğer 'başka' ihtiyacım yok eğer çizgi"
  • , kullanın Yüklü. pitonun Zen, Python PEP8 ve bazı programlama standartları ve stil için

kontrol.kod yapmak için

İlgili konular