当前位置: 首页 > article >正文

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

博主原博客地址:https://www.lisok.cn/Front-End/610.html

调用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
    }
  }

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

在这里插入图片描述

需要修改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


http://www.kler.cn/a/577926.html

相关文章:

  • vue和easyui渲染问题
  • 前端知识点---http.createHttp()的理解(arkts)
  • 求职面试常见问题:数组与链表
  • 【Linux内核系列】:深入解析输出以及输入重定向
  • 基于 ChatGPT 创建专属 GPTs
  • flink集成tidb cdc
  • 理解Java泛型通配符:List<? extends T>与List<? super T>的深度解析
  • Django模型数据修改:详解两种方式
  • 阿里巴巴全新推理模型QwQ-32B:性能比肩DeepSeek-R1,开源引领未来
  • 文本处理Bert面试内容整理-BERT的优点是什么?
  • 数据结构【AVL树(平衡二叉树)】
  • 一体式 IO 模块助力 PLC,开启纺织机自动化高效新篇章
  • MYOJ_4204:迷宫(图论-网格图基础,dfs,bfs在网格图中应用)
  • 【蓝桥杯集训·每日一题2025】 AcWing 5538. 回文游戏 python
  • React-路由配置
  • 读书会-c#并发编程
  • C++ 二叉搜索树代码
  • 支付宝当面付java,php,sdk下载
  • 14.DS18B20温度传感器
  • springboot + mybatis