python - 使用迭代写入文本文件

时间:2015-11-21 01:00:54

标签: python file iteration writing

我的代码不会写入文件,我做错了什么?请帮忙。我正在尝试编程以继续询问产品,直到用户没有输入产品代码。我希望将所有产品保存在文件中。

    store_file = open("Database.txt", "w")
    NewProduct = ""
    while NewProduct != False:
        contine = input("Press 1 to enter a new product press 2 to leave: ")
        if contine == "1":
            print("Enter your product information")
            information = []
            product = input("What's the product code: ")
            information.append(product)
            description = input("Give a description of the product: ")
            information.append(description)
            price = input("Enter price of product: ")
            information.append(price)
            information = str(information)
            clean = information.replace("]","").replace("[","").replace(",","").replace("'","")

          store_file.write(clean)

         elif contine == "2":
         NewProduct = False

        else:
          print("Your input is invalid")
    store_file.close

2 个答案:

答案 0 :(得分:0)

我让程序正在进行以下调整。请参阅注释以获取解释:

store_file = open("Database.txt", "w")
NewProduct = ""
while NewProduct != False:
    continue = raw_input("Press 1 to enter a new product press 2 to leave: ") 
    #Changed to raw_input because input was reading in an integer for 1 rather than a 
    #string like you have set up. This could be specific to my IDE
    if continue == "1":
        print("Enter your product information")
        information = []
        product = raw_input("What's the product code: ")
        information.append(product)
        description = raw_input("Give a description of the product: ")
        information.append(description)
        price = raw_input("Enter price of product: ")
        information.append(price)
        information = str(information)
        clean = information.replace("]","").replace("[","").replace(",","").replace("'","")

        store_file.write(clean + "\n") 
        #Added a line break at the end of each file write


    elif contine == "2":
        NewProduct = False

    else:
        print("Your input is invalid")
    store_file.close() #Added parentheses to call the close function

答案 1 :(得分:0)

我假设这里的问题是您使用的是Python 2,而input并没有按照您的想法行事。在Python 2中,input eval输入就好像它是Python源代码一样,所以如果有人输入2,它将返回int2,而不是"2"。在Python 2中,您希望始终使用raw_inputeval - 随机用户输入不安全/可靠。)

此外,在CPython(参考解释器)上,当文件超出范围时,文件往往会自然地关闭,你努力close,但忘了实际调用 close方法; store_file.close在不调用方法的情况下查找方法,store_file.close()实际上会关闭它。当然,明确的close通常是错误的方法;你应该使用with语句来避免忘记关闭(或跳过close的异常)。你可以替换:

store_file = open("Database.txt", "w")
...
store_file.close()

使用:

with open("Database.txt", "w") as store_file:
    ... do all your work that writes to the file indented within the with block ...
... When you dedent from the with block, the file is guaranteed to be closed ...

但是还有其他问题。你正在做什么:

information = str(information)
information = information.replace("]","").replace("[","").replace(",","").replace("'","")

太可怕了。我99%肯定你真正想要的只是加入带空格的输入。如果您将所有input次调用切换为raw_input(仅在Python 2上,在Python 3上,input与Python上的raw_input类似),那么您的{{1} }是list的列表,您可以str将它们放在一起,而不是尝试对join本身进行字符串化,然后删除所有list - y位。您可以使用以下内容替换上面的两行:

list