简体   繁体   English

有谁知道为什么无法写入.txt文件?

[英]Does anyone know why this writing to .txt file doesn't work?

When I run this code and input values for crop and quantity it doesn't write them to crop database.txt like it should, but just creates crop database.txt as a new blank text file. 当我运行此代码并输入cropquantity值时,它不会像应有的那样将它们写入crop database.txt .txt,而只是将crop database.txt .txt创建为新的空白文本文件。 Does anyone know why this is? 有人知道为什么是这样吗? Is it because of the way I am closing the file? 是因为我关闭文件的方式吗?

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

with open ('cropdatabase.txt', 'a+') as file:

 lines = file.readlines()

file.close ()

with open ('cropdatabase.txt', 'a+') as file:

 for row in lines:

    if crop in row:

       row = str(a)
       split_2 = a.split (',')
       split_2.append (quantity)

    else:

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

file.close ()

If you start with an empty file (or non-existing) the list lines will be empty. 如果你开始与一个空文件(或不存在的)名单lines将是空的。 So your code for writing anything never runs. 因此,您编写任何代码的代码永远不会运行。

First you need to use raw_input 首先,您需要使用raw_input

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

Then you do not need f.close() because you have with open. 这样就不需要f.close()了,因为您有open。

Hahaha small bug. 哈哈哈小虫子。

  • use open(filename, 'r') for reading a file 使用open(filename,'r')读取文件
  • use open(filename, 'a') for appending some data to a exsisting file 使用open(filename,'a')将一些数据附加到现有文件中
  • use open(filename, 'w') for creating/re-writing a file. 使用open(filename,'w')创建/重写文件。

when you open a file with appending 'a', state the file pointer is placed just before EOF(end of file) pointer. 当您打开带有'a'的文件时,请说明文件指针恰好位于EOF(文件结尾)指针之前。 So you wont be reading any data. 这样您就不会读取任何数据。

I would prefer nt using 'a+', it looks great but using a specific format gives more clarity for reading code. 我更喜欢nt使用'a +',它看起来很棒,但是使用特定格式可以使代码阅读更加清晰。

try this 尝试这个

with open ('cropdatabase.txt', 'r') as file:

   lines = file.readlines()

Hope this helps. 希望这可以帮助。

Here is your code with comments: 这是带有注释的代码:

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

If you are using python 2, this is a bad idea. 如果您使用的是python 2,这是个坏主意。 use raw_input 使用raw_input

with open ('cropdatabase.txt', 'a+') as file:

You are opening this file to append, but are just reading it 您正在打开要附加的文件,但正在阅读

 lines = file.readlines()

file.close ()

You don't need to close since you used 'with open' 您无需关闭,因为您使用过“ with open”

with open ('cropdatabase.txt', 'a+') as file:

Once again you open to append. 再次打开以追加。 You should rewrite the file with 'w' 您应该用“ w”重写文件

 for row in lines:

    if crop in row:

       row = str(a)
       split_2 = a.split (',')
       split_2.append (quantity)

This makes no sense to me. 这对我来说毫无意义。 What is a? 什么啊
After mucking with split2 (again, why) you never write anything 在分解split2之后(再次为什么),您再也不会写任何东西

    else:

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

Now you write crop and quantity to lines that don't have a match? 现在,您将作物和数量写入不匹配的行吗?

file.close ()

again, unnecessary close 再次,不必要的关闭

here is my code 这是我的代码

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

using raw_input, both will be strings 使用raw_input,两者都是字符串

with open ('cropdatabase.txt', 'r') as file:
    lines = file.readlines()

Read the complete file into lines, then close it (automatically) 将完整的文件读入行,然后关闭(自动)

with open ('cropdatabase.txt', 'w') as file:
    for row in lines:
        if crop in row:
            row = row.strip(), ", ", quantity
            file.write(row)

You don't specify, so I am writing the row that matches crop and adding the quantity after a comma 您未指定,因此我正在写与作物匹配的行,并在逗号后添加数量

        else:
            file.write(row)

Try this and come back with what you see. 尝试此操作,然后返回您看到的内容。 First save your input file to another name so you can compare the two 首先将输入文件保存为另一个名称,以便您可以比较两者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM