调用博主最近登录时间
生活中的HYGGE
解决Node Electron下调用Python脚本输出中文乱码的问题

解决Node Electron下调用Python脚本输出中文乱码的问题

hygge
2025-03-07 / 0 评论 / 15 阅读 / 正在检测是否收录...

解决Node Electron下调用Python脚本输出中文乱码的问题

调用Pyinstaller打包后的可执行文件方式如下:

import { promisify } from 'util'
import { exec } from 'child_process'
import { app } from 'electron'

async handleVerifyZy(id) {
    const entity = await this.findById(id)
    const execPromise = promisify(exec)
    let args = `-a 202501 -c community-sample.json -r resident-sample.json -t ${entity.timestamp}`
    const verifyOutput = await execPromise(
      `verify.exe ${args}`
    )
    // 正常运行结束需要取出结果的excel
    let outputExcelName = join(
      app.getPath('userData'),
      'verify_result',
      `${entity.timestamp}_monthly_zy_analysis_result.xlsx`
    )
    return {
      outputExcelName,
      verifyOutput
    }
  }

打印在渲染层发现标准输出的中文乱码

m7y8vjt1.png

需要修改Python脚本,定义全局的stdout的编码

# encoding:utf-8
import io
import sys

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding="utf-8")

打包后再次调用,问题解决

参考

1.nodejs调用python输出中文乱码的问题:https://blog.csdn.net/qq_40185480/article/details/105910329

0

评论 (0)

取消