简体   繁体   English

与cp1252 consoless上utf-8中的进程通信

[英]communicate with a process in utf-8 on a cp1252 consoless

I need to control a program by sending commands in utf-8 encoding to its standard input. 我需要通过以utf-8编码向其标准输入发送命令来控制程序。 For this I run the program using subprocess.Popen() : 为此,我使用subprocess.Popen()运行程序:

proc = Popen("myexecutable.exe", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
proc.stdin.write(u'ééé'.encode('utf_8'))

If I run this from a cygwin utf-8 console, it works. 如果我从cygwin utf-8控制台运行它,则它可以工作。 If I run it from a windows console (encoding ='cp1252') this doesn't work. 如果我从Windows控制台(编码='cp1252')运行它,则无法正常工作。 Is there a way to make this work without having to install a cygwin utf-8 console on each computer I want it to run from ? 有没有一种方法可以使此工作而不必在要运行的每台计算机上安装cygwin utf-8控制台? (NB: I don't need to output anything to console) (注意:我不需要向控制台输出任何内容)

I wonder if this caveat, from the subprocess documentation , is relevant: 我想知道子流程文档中的此警告是否相关:

The only reason you would need to specify shell=True on Windows is where the command you wish to execute is actually built in to the shell, eg dir, copy. 在Windows上需要指定shell = True的唯一原因是您希望执行的命令实际上内置在shell中,例如dir,copy。 You don't need shell=True to run a batch file, nor to run a console-based executable. 您不需要shell = True来运行批处理文件,也不需要运行基于控制台的可执行文件。

Why do you need to force utf-8 pipes? 为什么需要强制使用utf-8管道? Couldn't you do something like 你不能做类似的事情

import sys
current_encoding = sys.stdout.encoding
...
proc.stdin.write(u'ééé'.encode(current_encoding))

EDIT: I wrote this answer before you edited your question. 编辑:在您编辑问题之前,我写了这个答案。 I guess this is not what you're looking for, then, is it? 我想这不是您想要的,是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM