【问题标题】:python - writing to text file using iterationpython - 使用迭代写入文本文件
【发布时间】:2015-11-21 01:00:53
【问题描述】:

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

    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

【问题讨论】:

  • 这是 Python 2 还是 Python 3?有很大的不同;在 Py2 上,此代码保证说所有输入都是无效的。不过,我们需要知道实际错误(或缺少错误)才能确定。
  • 我正在使用 python 3。没有错误出现,但问题是创建了 database.txt 文件但不包含任何数据。

标签: python file iteration writing


【解决方案1】:

我让程序进行了以下调整。解释见 cmets:

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

【讨论】:

  • 感谢您的回答,感谢您的回答,我现在已经能够解决和改进我的代码。我正在使用 python 3 和它的新手。
【解决方案2】:

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

此外,虽然在 CPython(参考解释器)上,文件在超出范围时往往会自然关闭,但您尝试使用 close,但实际上忘记调用 @987654331 @ 方法; 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 2 上的raw_input),那么您的liststr 的列表,并且您可以将join 放在一起,而不是尝试对list 本身进行字符串化,然后删除所有list-y 位。您可以将上面的两行都替换为:

information = ' '.join(information)

【讨论】:

  • 感谢您的回答,感谢您的回答,我现在已经能够解决和改进我的代码。我正在使用 python 3 和它的新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多