2016-04-10 51 views
1

下面的代碼背後的想法是,如果變量crop已經包含在.txt文件中,變量quantity將被添加到與crop相同的行的末尾。這是我嘗試這樣做的,但它不起作用:你真的需要運行它來理解,但是,本質上,列表的錯誤部分被添加到了,不斷擴展的'/'系列出現並且行休息消失。有誰知道如何修改此代碼,使其正常工作?爲什麼不寫這個文件在python中工作?

應該怎樣輸出:

Lettuce 77 88 100 
Tomato 99 

實際輸出什麼:

["['\\n', 'Lettuce 77 \\n88 ', 'Tomato 88 ']100 "] 

代碼:

def appendA(): 

with open('alpha.txt', 'r') as file_1: 
    lines = file_1.readlines() 

    for line in lines: 
    if crop in line: 
     index = lines.index(line) 
     line = str(line + quantity + ' ') 
     lines [index] = line 
     newlines = str(lines) 

     #The idea here is that the variable quantity is added onto the end 
     # of the same row as the entered crop in the .txt file. 


     with open('alpha.txt', 'w') as file_3: 
      file_3.write (newlines) 

def appendB(): 

with open('alpha.txt', 'a') as file_2: 

    file_2.write ('\n') 
    file_2.write (crop + ' ') 
    file_2.write (quantity + ' ') 

crop = input("Which crop? ") 
quantity = input("How many? ") 

with open('alpha.txt', 'a') as file_0: 

if crop in open('alpha.txt').read(): 
    appendA() 
else: 
    appendB() 
+0

你可以發佈一個「示例正確的輸出」,而你取而代之嗎? – syntonym

+1

您需要在輸入時從輸入中去除換行符,而且這不是在python中遍歷文本文件的最佳方式。 –

+0

請勿張貼文字的屏幕截圖。這很愚蠢。 – Tomalak

回答

0

讓我們開始吧!你的代碼應該是這樣的:

def appendA(): 
    with open('alpha.txt', 'r') as file_1: 
     lines = [] 

     for line in file_1: 
      if crop in line: 
       line = str(line.rstrip("\n") + quantity + "\n") 
      lines.append(line) 

     #The idea here is that the variable quantity is added onto the end 
     # of the same row as the entered crop in the .txt file. 
     with open('alpha.txt', 'w') as file_3: 
      file_3.writelines(lines) 


def appendB(): 
    with open('alpha.txt', 'a') as file_2: 
     file_2.write('\n') 
     file_2.write(crop + ' ') 
     file_2.write(quantity + ' ') 

crop = "Whichcrop" 
quantity = "+!!!+" 

with open('alpha.txt') as file_0: 
    if crop in file_0.read(): 
     print("appendA") 
     appendA() 
    else: 
     print("appendB") 
     appendB() 


with open('alpha.txt', 'a') as file_0: 
    if crop in open('alpha.txt').read(): 
     appendA() 
    else: 
     appendB() 

你也犯了幾個錯誤。 這行代碼以open('alpha.txt','a')作爲file_0:「打開文件,上下文附加在文件末尾,但不使用變量file_0。我認爲這是額外的。 在下一步你打開文件檢查「裁剪在打開('alpha.txt')。read()」,但從不關閉它。

[「[ '\ n', '生菜77 \ N88',「番茄88 '] 100「] 因爲,你用寫的,而不是writelines你得到這樣的輸出: 開放(' alpha.txt ','w')as file_3: file_3.write(換行符)

此外,您還可以在每次迭代後寫入文件,以形成字符串列表,然後寫入文件。

0
newlines = str(lines) # you convert all lines list to str - so you get default conversion 

而且如果你想你應該更換整個文件寫在中間

而且你還可以得到讀取appendB的,因爲你還檢查每一個線和你的代碼反正不是最佳的性能:)方面

from os import remove, close 

def appendA(filename, crop, quantity): 

    result = [] 
    exists = False 
    with open(filename, 'r') as file_1: 
     lines = file_1.readlines() 

    for line in lines: 
     if not crop in line: 
      result.append(line) 
     else: 
      exists = True 
      result.append(line.strip('\n') + quantity + '\n') 


    if not exists:   
     with open(filename, 'a') as file_2: 
      file_2.write ('\n' + crop + ' ' + quantity + ' ') 
    else: 
     tmp_file = filename + '.tmp' 
     with open(tmp_file, 'w') as file_3: 
      file_3.write(result) 
      remove(filename) 
      move(tmp_file, filename) 
0
  1. 「str(lines)」:行是列表類型,可以使用''.join(行)到 將其轉換爲字符串。
  2. 「在線線」:「行」結束與「\ n」個
  3. 代碼縮進錯誤:「行換行=‘’。加入(系)」,並且跟隨
  4. 「如果在作物行」是錯誤的,如果作物命名爲「AA」和「AABB」,則新的輸入「AA」返回true,則數量將被附加到包括「AA」的所有行的 ,不僅是「AA」行。


    def appendA(): 
     with open('alpha.txt', 'r') as file_1: 
      lines = file_1.readlines() 

      for line in lines: 
       if crop in line: 
        index = lines.index(line) 
        line = str(line.replace("\n", "") + ' ' + quantity + '\n') 
        lines[index] = line 
      newlines = ''.join(lines) 

      # The idea here is that the variable quantity is added onto the end 
      # of the same row as the entered crop in the .txt file. 
      with open('alpha.txt', 'w') as file_3: 
       file_3.write(newlines) 


    def appendB(): 
     with open('alpha.txt', 'a') as file_2: 
      file_2.write("\n") 
      file_2.write(crop + ' ') 
      file_2.write(quantity + ' ') 


    crop = input("Which crop? ") 
    quantity = input("How many? ") 

    with open('alpha.txt', 'a') as file_0: 
     if crop in open('alpha.txt').read(): 
      appendA() 
     else: 
      appendB()