简体   繁体   English

Python - 将输入附加到 .txt 但删除先前的数据

[英]Python - Append input to .txt but deletes prior data

I´m trying to append input to a .txt and succeed to do so, however the data previously present in my .txt file gets overwritten by the data inputted.我正在尝试将输入附加到 .txt 并成功执行此操作,但是之前存在于我的 .txt 文件中的数据被输入的数据覆盖。 I wish for the data to stay with the input added.我希望数据与添加的输入保持一致。

Heres my code so far:到目前为止,这是我的代码:

def tilføjMedlem():
    while True: 
        tilføjFornavn = input("Skriv fornavn på det medlem du ønsker at tilføje: (Tast q for at afslutte)")
        if tilføjFornavn == 'q':
            break
        else:
            tilføjEfternavn= input("Efternavn:")
            tilføjJollypoints= input("Jollypoints:")
            tilføjInstagram = input("Instagram:")
            tilføjMedlemSiden = input('Medlem siden:')

    with open('fans.txt', 'a') as text_file:
        text_file.write(tilføjFornavn+" "+tilføjEfternavn+" "+tilføjJollypoints+" "+tilføjInstagram+" "+tilføjMedlemSiden)

tilføjMedlem()

You need to fix the indentation of the with open() function.您需要修复with open()函数的缩进。

Your code is re-assigning the values to variables you have declared.您的代码正在将值重新分配给您声明的变量。 It will call write to file function only once with the last values entered in the variables after user had entered q to quit.在用户输入q退出后,它只会调用一次写入文件函数,并使用变量中输入的最后一个值。

def tilføjMedlem():
    while True: 
        tilføjFornavn = input("Skriv fornavn på det medlem du ønsker at tilføje: (Tast q for at afslutte)")
        if tilføjFornavn == 'q':
            break
        else:
            tilføjEfternavn= input("Efternavn:")
            tilføjJollypoints= input("Jollypoints:")
            tilføjInstagram = input("Instagram:")
            tilføjMedlemSiden = input('Medlem siden:')

        with open('fans.txt', 'a') as text_file:
            text_file.write(tilføjFornavn+" "+tilføjEfternavn+" "+tilføjJollypoints+" "+tilføjInstagram+" "+tilføjMedlemSiden)

tilføjMedlem()

The code work代码工作

Actually, the code append the text correctly to the .txt file.实际上,代码将文本正确附加到 .txt 文件中。 But it overwrite the previous input you may enter in the while loop.但它会覆盖您可能在while循环中输入的先前输入。 If the input getter behave like intended, then there is no issues with your code.如果输入 getter 的行为符合预期,那么您的代码就没有问题。 It get the 4 last inputs you enter, then append them in the file without overwriting it:它获取您输入的最后 4 个输入,然后append它们append到文件中而不覆盖它:

however the data previously present in my .txt file gets overwritten by the data inputted但是之前存在于我的 .txt 文件中的数据被输入的数据覆盖

This is false.这是错误的。 Your code works.您的代码有效。 Here is what I executed:这是我执行的:

def tilføjMedlem():
    while True:
        tilføjFornavn = input("Skriv fornavn på det medlem du ønsker at tilføje: (Tast q for at afslutte)")
        if tilføjFornavn == 'q':
            break
        else:
            tilføjEfternavn= input("Efternavn:")
            tilføjJollypoints= input("Jollypoints:")
            tilføjInstagram = input("Instagram:")
            tilføjMedlemSiden = input('Medlem siden:')

    with open('fans.txt', 'a') as text_file:
        text_file.write(tilføjFornavn+" "+tilføjEfternavn+" "+tilføjJollypoints+" "+tilføjInstagram+" "+tilføjMedlemSiden)

tilføjMedlem()

Original file:原始文件:

This is the original data这是原始数据

Script execution:脚本执行:

Skriv fornavn på det medlem du ønsker at tilføje: (Tast q for at afslutte)
>>>? n
Efternavn:
>>>? First input
Jollypoints:
>>>? second input
Instagram:
>>>? third input
Medlem siden:
>>>? and this is the last one.
Skriv fornavn på det medlem du ønsker at tilføje: (Tast q for at afslutte)
>>>? q

Final file:最终文件:

This is the original data这是原始数据

q First input second input third input and this is the last one. q 第一个输入第二个输入第三个输入,这是最后一个。

If the intended behavior is that EVERY inputs append to the file, you have to change the indent of the with block:如果预期的行为是每个输入都附加到文件中,则必须更改with块的缩进:

def tilføjMedlem():
    while True:
        tilføjFornavn = input("Skriv fornavn på det medlem du ønsker at tilføje: (Tast q for at afslutte)")
        if tilføjFornavn == 'q':
            break
        else:
            tilføjEfternavn= input("Efternavn:")
            tilføjJollypoints= input("Jollypoints:")
            tilføjInstagram = input("Instagram:")
            tilføjMedlemSiden = input('Medlem siden:')

        with open('fans.txt', 'a') as text_file:
            text_file.write(tilføjFornavn+" "+tilføjEfternavn+" "+tilføjJollypoints+" "+tilføjInstagram+" "+tilføjMedlemSiden)

tilføjMedlem()

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

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