简体   繁体   English

使用\\ r \\ n时,Python额外的行写操作(在VS Code中)

[英]Python extra line write when using \r\n (in VS Code)

Very new to Python and am following a video tutorial online for learning Python, using VS Code. Python的新手,正在网上观看视频教程,使用VS Code学习Python。 The following code is being used to generate a text file: 以下代码用于生成文本文件:

#
# Read and write files using the built-in Python file methods
#

def main():  
  # Open a file for writing and create it if it doesn't exist
  f = open("textfile.txt", "w+")

  # Open the file for appending text to the end


  # write some lines of data to the file
  for i in range(10):
    f.write("This is line " + str(i) + "\r\n")

  # close the file when done
  f.close()


  # Open the file back up and read the contents


if __name__ == "__main__":
  main()

When I run it, I get: 当我运行它时,我得到:

This is line 0

This is line 1

This is line 2

This is line 3

This is line 4

This is line 5

This is line 6

This is line 7

This is line 8

This is line 9

Whereas, the tutor's output is: 导师的输出为:

This is line 0
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9

I have tried experimenting with the "\\r\\n" and when using either one or the other(as "\\r" or "\\n"), I get the correct response, but with both together it results in the extra line. 我尝试使用“ \\ r \\ n”进行试验,当使用其中一个(如“ \\ r”或“ \\ n”)时,我得到了正确的响应,但同时使用它们会导致额外的一行。

I have read somewhere that there is a difference between Windows and Mac for this, but I couldn't find a whole load of details about what these characters are and what they do. 我在某处读到了Windows和Mac的区别,但是我找不到关于这些字符是什么以及它们做什么的全部细节。

EDIT: I am using Windows, the tutor was using Mac 编辑:我正在使用Windows,导师正在使用Mac

newline controls how universal newlines works (it only applies to text mode). newline控制通用换行符的工作方式(仅适用于文本模式)。 It can be None, '', '\\n', '\\r', and '\\r\\n'. 可以是“无”,“”,“ \\ n”,“ \\ r”和“ \\ r \\ n”。 It works as follows: 其工作方式如下:

On input, if newline is None, universal newlines mode is enabled. 输入时,如果换行符为“无”,则启用通用换行符模式。 Lines in the input can end in '\\n', '\\r', or '\\r\\n', and these are translated into '\\n' before being returned to the caller. 输入中的行可以以'\\ n','\\ r'或'\\ r \\ n'结尾,在返回给调用者之前,这些行会转换为'\\ n'。 If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. 如果为”,则启用通用换行模式,但行结尾不翻译就返回给呼叫者。 If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. 如果它具有任何其他合法值,则输入行仅由给定的字符串终止,并且该行的末尾未经转换返回给调用方。

On output, if newline is None, any '\\n' characters written are translated to the system default line separator, os.linesep. 在输出中,如果换行符为None,则任何写入的'\\ n'字符都将转换为系统默认的行分隔符os.linesep。 If newline is '', no translation takes place. 如果换行符是“”,则不会进行翻译。 If newline is any of the other legal values, any '\\n' characters written are translated to the given string. 如果换行符是其他任何合法值,则将写入的所有“ \\ n”字符转换为给定的字符串。

That means since python 3.2 any newline character (in your case \\r and \\n) is translated to os.linsep which is the line seperator of your os 这意味着自python 3.2起,任何换行符(在您的情况下为\\ r和\\ n)都被翻译为os.linsep ,这是操作系统的行分隔符

Source is the python documentation: https://docs.python.org/release/3.2/library/functions.html#open 来源是python文档: https : //docs.python.org/release/3.2/library/functions.html#open

On Windows, writing \\n is translated to \\r\\n when the file is opened in text mode (the default), so writing \\r\\n gives you \\r\\r\\n. 在Windows上,当以文本模式(默认)打开文件时,将\\ n转换为\\ r \\ n,因此写入\\ r \\ n会为您\\ r \\ r \\ n。 Write \\n only in text mode, which is portable and the correct end of line for the OS is used. 仅在可移植的文本模式下写入\\ n,并且使用了操作系统的正确行尾。

You just need to change the open line to: 您只需要将打开的行更改为:

f = open("textfile.txt", "w+",newline='')

In case you are using python 2.x then do: 如果您使用的是python 2.x,请执行以下操作:

f = open("textfile.txt", "wb+")

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

相关问题 Python“\\ n”标记额外的行 - Python “\n” tag extra line 为什么 \r\n output 变成 \r\r\n 使用 python 写入文件 - Why \r\n output become \r\r\n using python write to file \ r \ n vs \ n在python eval函数中 - \r\n vs \n in python eval function python 3 在写入文件时将换行符 \r 替换为 \n - python 3 replaces line break \r with \n when writing to a file Python:使用write()时,为什么输出中有额外的空行? - Python: Why are there extra blank lines in the output when using write()? python line strip \\ n和write line - python line strip \n and write line 从servlet返回的HttpsUrlConnection响应在通过python库读取时包含额外的'b''0''\\ r \\ n'字符 - HttpsUrlConnection response returned from servlet contains extra 'b''0''\r\n' characters when read through python library 在 Mac 与 Windows 上的 Python 中处理 \\r\\n 与 \\n 换行符 - Handling \r\n vs \n newlines in python on Mac vs Windows 使用write函数写入文件python时断行 - Line breakage when using write function to write to a file python 如何强制 Python 的 file.write() 在 Windows 中使用与 Linux 中相同的换行符格式(“\\r\\n”与“\\n”)? - How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n” vs. “\n”)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM