python - 为什么 Python 在打开 utf-16 文件时不读取行尾字符?
问题描述
我正在连接两个文本文件,一个是 utf-16。从文件中读取行并将它们拆分时,utf-16 文件没有行尾。一切都在一行中,所以我必须指定一个行尾字符。任何想法为什么?
下面的代码正在运行,但我想知道为什么我需要为 utf-16 设置行尾。
with open(file_temp, 'w') as outfile:
with open(file_normal) as infile:
for line in infile:
outfile.write(line.split(",")[0]) # auto end of line
with open(file_utf16, encoding='utf-16') as infile: # different file format
for line in infile:
outfile.write(line.split(",")[0] + "\n") # needs end of line char for some reason ?
当以正确的编码读取时,我希望行尾字符出现在 utf-16 文件中。
解决方案
换行符与编码无关
with open("someFile_utf16.txt", "w",encoding='utf-16') as infile:
for x in range(10):
infile.write(str(x))
with open("someFile_normal.txt", "w") as infile:
for x in range(10):
infile.write(str(x))
两者在文件中都有相同的数据
0123456789
唯一可能的解释是普通文件写入了行尾,而 utf-16 文件没有
更多参考