简体   繁体   English

使用 Python 编写文件时换行符 (\n) 重复?

[英]Newline character (\n) duplicates when writing a file with Python?

I'm writing a simple program that sends bytes to a server (UDP connection), the server echoes the bytes back, and then I write the echo on an output file.我正在编写一个简单的程序,它将字节发送到服务器(UDP 连接),服务器回显字节,然后我将回显写入输出文件。 The main functions are these:主要功能如下:

def communication_into_server(s, size, filename):
    f = open(filename, 'rb')
    while True:
        line = f.read(size)
        if line == b'':
            break
        s.send(line)
    f.close()
    s.send(b"EOF")

def communication_from_server(s, size):
    g = open("output.txt", 'w', encoding="utf-8")
    while True:
        line = s.recv(size)
        print(line.decode("utf-8"))
        if line == b'EOF':
            break
        g.write(line.decode("utf-8"))
    g.close()

Some sample text is this:一些示例文本是这样的:

This is some sample text
with a single newline character.

and its output looks like this:它的输出如下所示:

This is some sample text

with a single newline character.

Apparently, \n characters are being duplicated somewhere.显然, \n 字符在某处被复制。 Note the print() on the second function.请注意第二个函数上的 print()。 It prints this:它打印这个:

This is some sample text
with a single newline character.

without the extra newline character, so the problem doesn't appear to be in the connection.没有额外的换行符,所以问题似乎不在连接中。 If I change 'w' to 'a' while opening the output file, nothing changes.如果我在打开输出文件时将“w”更改为“a”,则没有任何变化。 What am I missing?我错过了什么?

I had the same problem, saving markdown files using python, and the spaces were being duplicated on each save.我遇到了同样的问题,使用 python 保存 markdown 文件,并且每次保存时都会复制空格。 I simply encoded the string before saving it in a file using the encode method:我只是在使用encode方法将字符串保存到文件之前对字符串进行了编码:

body = body.encode()
# now it can be saved into a file...

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

相关问题 编写.txt文件Python时,换行符“\ n”不起作用 - Newline “\n” not Working when Writing a .txt file Python 为了节省空间,哪种字符编码最适合写入 Python 中的文件,仅用于位和换行符 (0, 1, '\n')? - What character encoding is best for writing to a file in Python for only bits and newline (0, 1, '\n'), for space-conserving purposes? Python 中的 JSON 转储在文件中写入换行符和回车符。 - JSON dump in Python writing newline character and carriage returns in file. 打印 Python 字典时,在换行符 (\n) 前面出现附加 \ - Additional \ appearing in front of newline character(\n) when print Python dictionary Python:使用 \n 作为换行符读取文件。 文件还包含 \r\n - Python: Reading a file by using \n as the newline character. File also contains \r\n 即使在写入文件后,文件末尾也没有换行符 - No newline character at end of file even after writing it Python转义字符与反斜杠n混合换行 - Python escape character mixed with backslash n for newline 写入文件时,Python处理换行符和制表符 - Python handling newline and tab characters when writing to file Python - 写入文件时删除最后一个字符 - Python - remove last character when writing to a file 将\\ n写入.txt文件不会换行,只是说\\ n - Writing \n to .txt file does not make a newline, just says \n
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM