&引用;charmap编解码器可以';t编码字符“;Python3中的西里尔字母错误

&引用;charmap编解码器可以';t编码字符“;Python3中的西里尔字母错误,python,node.js,utf-8,character-encoding,Python,Node.js,Utf 8,Character Encoding,我正在从事一个个人项目,其中我有一个webapp,其中前端(React)将一些数据传递给后端节点。对于我的特定用例,我需要在python脚本中使用这些数据 为了实现这一点,我在nodeJS服务器文件中具有以下功能。该函数设置一个API端点,将数据写入json文件。我的数据包含西里尔字符,因此在写入JSON文件时,我将数据编码为UTF-8。python文件作为节点子进程启动,并通过stdout设置一些日志记录: app.post('/add',interfaceAnki); //Function

我正在从事一个个人项目,其中我有一个webapp,其中前端(React)将一些数据传递给后端节点。对于我的特定用例,我需要在python脚本中使用这些数据

为了实现这一点,我在nodeJS服务器文件中具有以下功能。该函数设置一个API端点,将数据写入json文件。我的数据包含西里尔字符,因此在写入JSON文件时,我将数据编码为UTF-8。python文件作为节点子进程启动,并通过stdout设置一些日志记录:

app.post('/add',interfaceAnki);
//Function which passes the JSON to anki itself through python
function interfaceAnki(req,res) {

  const launchPython = () => {
    console.log("launching python child process");
    var spawn = require("child_process").spawn;
    var py = spawn(path.join(__dirname, "../scripts/env/Scripts/python"),["-u",path.join(__dirname, "../scripts/anki_service.py")]);

    py.stdout.on('data', function(data) {
      console.log(data.toString());
    });

    py.stderr.on('data', function (data){
      console.log(data.toString());
    });
  }

  fs.writeFile(path.join(__dirname, "../scripts/datasource.json"), JSON.stringify(req.body),{encoding: 'utf8'},launchPython);  
  res.json({ msg: 'success' });  
}  
此JSON文件内容的典型示例如下:

{"imageURL":"https://pixabay.com/get/g7fcf38edfbbf40f130e3aa88902ab0ab804f35e3caf48c33b513114889b49afbbdd7fdc7e4ea49f7dff708476fe73e21758ae8733a9db7b745dafccfe10048e0_640.jpg","accented":"соба'ка","pronounciationURL":"https://api.openrussian.org/read/ru/соба'ка","exampleSentence":"Мне нра́вятся соба́ки, а мое́й сестре́ кошки.","extraInfo":"feminine gender"}
然而,我在python端的编码方面遇到了困难。我的python脚本将.JSON文件中的数据读入字典

def read_in():
    path = os.path.dirname(os.path.abspath(__file__))+'\datasource.json'
    with open(path,encoding="utf-8") as json_file:
        try:
            data = json.load(json_file)
            print(data["pronounciationURL"])
            return data
        except Exception as e:
            print("An error occured in reading the json datasource: ")
            print(e)
            sys.exit()
但是,行
print(数据[“发音URL”])
抛出以下错误:

An error occured in reading the json datasource: 
'charmap' codec can't encode characters in position 36-39: character maps to <undefined>
此函数称为:

download_file('audio',data["pronounciationURL"],data["accented"])
在尝试调用此函数时,我再次遇到相同的错误,更具体地说是从
open()

下载音频时发生错误 “charmap”编解码器无法对位置50-53中的字符进行编码:字符映射到
其他人问过这个错误,但是我尝试过添加
encoding='utf-8'
的建议,但没有成功。总的来说,我想首先理解为什么会发生这种错误,也许更好的方法是使用西里尔字母字符——可能是我的编码应该来自源代码(nodeJS),以确保在转换到python之前语言之间的交叉兼容性。我似乎在处理nodeJS中的数据时没有任何问题,只有python3。

您试图在
download\u file
中打开的文件路径是否包含西里尔语或其他非拉丁语字符?是的,我基本上是从远程源下载俄语单词的音频文件。远程源在路径名中包含西里尔文,我还使用西里尔文这个词在本地保存文件。例如,/downloads/Сааааааа.mp3似乎表明,将控制台更改为处理unicode可能是解决方案。看到和
download_file('audio',data["pronounciationURL"],data["accented"])
An error occured downloading audio
'charmap' codec can't encode characters in position 50-53: character maps to <undefined>