为什么 Python 打开 utf-16 文件时不读取行尾字符?
Why does Python not read the end of line character when opening utf-16 file?
我正在连接两个文本文件,一个是 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文件没有
更多参考
我正在连接两个文本文件,一个是 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文件没有
更多参考