使用utf-8编码写入节点js文件中的度符号问题

问题描述 投票:0回答:1

我正在使用nodeJs npm fs模块使用utf-8编码读写文件。文件读写操作成功完成。但是,当我在Windows写字板中打开文件时,摄氏度(°C)符号显示为(°C)。如果我在VS代码编辑器中打开相同的文件,并且记事本摄氏度(°C)符号正确出现,则问题仅在于Windows写字板应用程序。

这里是节点Js中的文件写入代码。

var fs = require('fs');    
let data="Temperature is 25°C";
fs.writeFile("./output.txt", data, 'utf8', function (err) {
        if (err) {
          console.error("Error while writing the output file");
        } else {
          console.log("File write operation done!");
        }
});
node.js character-encoding fs wordpad
1个回答
0
投票

在我看来,就像写字板认为您的文件是用Latin1(iso-8859-1)或Windows-1252编码的。这些字符编码每个字符使用一个字节,因此会导致utf8 Unicode错误。

最佳解决方案:使用记事本或记事本++,然后亲吻写字板再见。

这是一种调整文件的方法,因此各种应用程序都知道它是unicode。 https://superuser.com/questions/1275925/make-wordpad-properly-open-a-utf-8-or-utf-16-encoded-file

© www.soinside.com 2019 - 2024. All rights reserved.