为什么打开utf-16文件时Python为什么不读取行尾字符?

时间:2019-06-21 06:27:19

标签: 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文件中会出现换行符。

1 个答案:

答案 0 :(得分:0)

换行符与这种编码无关

var promise = new Promise( (resolve, reject) => {
   var request = window.indexedDB.open("mydb");
   request.onsuccess = function (event) {
    var db = event.target.result;
    var objectStore = db.transaction("mytable").objectStore("mytable");
    allRecords =   objectStore.getAll();
    allRecords.onsuccess=  function(event){
      console.log(event.target.result)
       b = 10;
       b = event.target.result

    }

}
promise.then((a)=>console.log(a));

两个文件中的数据相同

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))

唯一可能的解释是普通文件中写入了行尾,而utf-16文件中没有

更多参考

https://docs.python.org/3/tutorial/inputoutput.html