简体   繁体   English

在txt文件中添加一行

[英]adding a line to a txt file

I am trying to add a line to the end of a txt file. 我正在尝试在txt文件的末尾添加一行。 I have been reading some posts here and trying differents options, but, for some reason, the new line is neved added after the last one, it is just appended next to the last one. 我一直在这里阅读一些文章,并尝试使用differents选项,但是由于某种原因,必须在最后一行之后添加新行,而将新行追加到最后一行之后。

So I was wondering what I am doing wrong....here I am showing my tests: 所以我想知道自己在做错什么。...在这里显示我的测试:

TEST 1: 测试1:

#newProt is a new data entered by the user in this case 12345
exists = False
f = open('protocols.txt', 'a+')
for line in f:
    if newProt == line:
        exists = True

if not exists:
    f.write(newProt)
f.close()

txt file after this code: 此代码后的txt文件:

2sde45 2sde45

21145 21145

we34z12345 we34z12345

TEST 2: 测试2:

exists = False
with open('protocols.txt', 'r+') as f:
    for line in f:
        if newProt == line:
            exists = True

    if not exists:
        f.write(newProt)

txt file after this code: exactly the same as above... 此代码后的txt文件:与上述完全相同...

And, like this, I have tested some combinations of letters to open the file, rb+, w, etc but for some reason I never get the desired output txt file: 而且,像这样,我已经测试了一些字母组​​合来打开文件,rb +,w等,但是由于某些原因,我从未获得所需的输出txt文件:

2sde45 2sde45

21145 21145

we34z we34z

12345 12345

So I do not know what I am doing wrong, I am following some examples I gor from some other posts here. 因此,我不知道自己在做错什么,我正在关注一些其他示例中的示例。

Try this: 尝试这个:

exists = False
f = open('protocols.txt', 'a+')
for line in f:
    if newProt == line:
        exists = True

if not exists:
    f.write('\n' + newProt)
f.close()

This adds the new line character to the end of the file then adds 'newProt'. 这会将新行字符添加到文件末尾,然后添加“ newProt”。

EDIT: 编辑:

The reason why your code did not produce the desired result is because you were simply writing a string to the file. 您的代码未产生预期结果的原因是因为您只是在文件中写入字符串。 New lines in text are not really 'in' the text file. 文本中的新行并不是真正地在文本文件中。 The text file is literally a series of bytes known as chars. 文本文件实际上是一系列称为char的字节。 The reason why various applications such as text editors show you new lines is because it interprets certain characters as formatting elements rather than letters or numbers. 诸如文本编辑器之类的各种应用程序向您显示换行符的原因是,它会将某些字符解释为格式元素,而不是字母或数字。

'\\n' is one such formatting character (in the ASCII standard), and it tells your favorite text editor to start a new line. '\\ n'是一个这样的格式字符(在ASCII标准中),它告诉您喜欢的文本编辑器开始新的一行。 There are others such as '\\t' which makes a tab. 还有其他诸如'\\ t'这样的标签。

Have a look at the wiki article on Newline character for more info 有关更多信息,请参见有关换行符的Wiki文章。

You can use f.seek(-x,x), reach the last line and then f.write(). 您可以使用f.seek(-x,x),到达最后一行,然后再使用f.write()。

Otherwise my understanding is if you open a file in "a" (append) mode, it'll anyways be written in the end 否则,我的理解是,如果您以“ a”(附加)模式打开文件,则该文件无论如何都将写在最后

Refer to this link: Appending line to a existing file having extra new line in Python 请参阅此链接: 将行追加到现有文件中,在Python中有额外的新行

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

相关问题 将数字写入 txt 文件的第二行,并在 Python 中添加数字 - Writing numbers to the second line of txt file wtih adding number on it in Python 使用 Python 在 a.txt 文件的每一行中添加“-”号 - Adding a “-” sign in each line of a .txt file using Python 向 .txt 文件的每一行添加(非重复)随机数 - Adding (non-repeating) Random Numbers to Each Line of .txt File 将数据添加到 txt 文件(并将其保存在那里) - Adding data to a txt file (and saving it there) Python:在txt文件中添加时间 - Python: Adding Time in a txt file 将.txt添加到已压缩的文件中 - Adding a .txt to an already zipped file 如何读取.txt文件,并在python中的每一行的特定位置/索引后添加空格 - How to reading .txt file , and adding space after specific position / index for each line in python 如何读取 .txt 文件,并在特定位置/索引后添加空格,为 python 中的每一行 - how to reading .txt file , and adding space after specific position/index , for each line in python 使用Python在TXT文件中的每个时间戳之前添加换行符 - Adding a line break before each time stamp in TXT file using Python 如何读取.txt文件并通过在python中的每一行的特定位置/索引后添加空格来重写 - How to reading .txt file and rewriting by adding space after specific position / index for each line in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM