2016-04-10 49 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()