Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 固定治疗'\n';作为本机EOL字符_Python_Eol - Fatal编程技术网

Python 固定治疗'\n';作为本机EOL字符

Python 固定治疗'\n';作为本机EOL字符,python,eol,Python,Eol,(Python 3.5.1) 我正在尝试创建一个EOL字符“normalizer”,因为它将获取一个文件,并将EOL字符更改为用户指定的字符 问题是,我认为python将“\n”视为本机EOL字符。例如,我在使用CRLF的Windows上,因此它将“\n”作为“\r\n”写入文件。(我用HxD确认了这一点) 这是我的密码: for line in file_in: line = line.rstrip("\r\n") #Strip EOL chars line += "\n" #

(Python 3.5.1)

我正在尝试创建一个EOL字符“normalizer”,因为它将获取一个文件,并将EOL字符更改为用户指定的字符

问题是,我认为python将“\n”视为本机EOL字符。例如,我在使用CRLF的Windows上,因此它将“\n”作为“\r\n”写入文件。(我用HxD确认了这一点)

这是我的密码:

for line in file_in:
    line = line.rstrip("\r\n") #Strip EOL chars
    line += "\n" #Add normalized EOL char
    file_out.write(line)

有没有办法让python将'\n'作为LF而不是用本机EOL字符替换?

如果以二进制模式打开文件(例如,
file\u out=open('somefile.txt',wb')
),它将只写出您给它的字节,而不进行换行翻译。您可能还希望以二进制模式打开输入文件,以确保在中读取时不会进行换行翻译