Python Popen无法在Windows PowerShell中使用正确的编码 [英] Python Popen failing to use proper encoding in Windows PowerShell

查看:122
本文介绍了Python Popen无法在Windows PowerShell中使用正确的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows PowerShell中运行我的Python脚本,该脚本应使用Popen运行另一个程序,然后通过管道将该程序的输出(实际上是Mercural)用于我的脚本.尝试在PowerShell中执行脚本时出现编码错误.

I am running my Python script in Windows PowerShell, and the script should run another program using Popen, then pipe the output of that program (Mercurial, actually) for use in my script. I am getting an encoding error when I try to execute my script in PowerShell.

我很确定这是会发生的,因为在获取Popen调用的输出时,Python没有使用PowerShell使用的正确编码. 问题是我不知道如何告诉Python使用正确的编码.

I am quite sure it is happening because Python is not using the correct encoding that PowerShell is using, when getting the output of the Popen call. The problem is that I don't know how to tell Python to use the correct encoding.

我的脚本看起来像

# -*- coding: utf-8 -*-
#... some imports
proc = Popen(["hg", "--cwd", self.path, "--encoding", "UTF-8"] + list(args), stdout=PIPE, stderr=PIPE)
#... other code

当我在Linux上运行此脚本时,我没有任何问题.我还可以使用PowerShell在Windows 7 Home Premium 64位中运行脚本,不会出现任何问题.此Windows 7中的PowerShell使用的是代码页850,即chcp的输出为850("ibm850").

When I run this script on Linux, I have no problems whatsoever. I can also run the script in Windows 7 Home Premium 64-bit using PowerShell with no problems. The PowerShell in this Windows 7 is using the code page 850, that is, the output of chcp is 850 ("ibm850").

但是,当我使用默认编码为cp437(chcp = 437 ),我从Python(版本2.7.2)中收到以下错误:

However, when I run the script in a Windows 7 Starter 32-bits using a PowerShell that has by default the encoding cp437 (chcp = 437), I get the following error from Python (version 2.7.2):

File "D:\Path\to\myscript.py", line 55, in hg_command
    proc = Popen(["hg", "--cwd", self.path, "--encoding", "UTF-8"] + list(args), stdout=PIPE, stderr=PIPE)
File "C:\Program files\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
File "C:\Program files\Python27\lib\subprocess.py", line 852, in _execute_child
    args = list2cmdline(args)
File "C:\Program files\Python27\lib\subprocess.py", line 615, in list2cmdline
    return ''.join(result)
UnicodeDecodeError: 'utf8' codec cant decode byte 0xe3 in position 0: unexpected end of data

我尝试了以下操作,但没有成功(即上述错误报告保持不变):

I have tried the following, with no success (i.e., the above error report stays the same):

  • 从脚本中删除# -*- coding: utf-8 -*-行.
  • 在脚本中删除-- encoding UTF-8选项,以通过Popen运行Mercurial.
  • 在执行脚本之前,在PowerShell中将编码更改为chcp 850.
  • 我在其他Stack Overflow答案中发现了许多其他Python黑客.
  • Remove the line # -*- coding: utf-8 -*- from my script.
  • Remove the -- encoding UTF-8 option for running Mercurial through Popen in my script.
  • Change the encoding to chcp 850 in PowerShell before executing my script.
  • Many other miscellaneous Python hacks I've found in other Stack Overflow answers.

有关我的特定详细信息,可以在BitBucket中的此处来获得我的整个源代码.. hgapi.py是给出错误的脚本.

For my specific details, my whole source code is available here in BitBucket. hgapi.py is the script that gives the error.

更新: 该脚本正在由其他脚本调用正在设置像这样的编码

UPDATE: The script is being called by this other script, which is setting the encoding like this

sys.setdefaultencoding("utf-8")

这一行看起来很重要,因为如果我将其注释掉,则会收到另一个错误:

This line looks important, because if I comment it out, I get a different error:

UnicodeDecoreError: 'ascii' codec cant decode byte 0xe3 in position 0: ordinal not in range(128)

推荐答案

使用from __future__ import unicode_literals后,我开始遇到相同的错误,但代码的不同部分:

After using from __future__ import unicode_literals I started getting the same error but in a different part of the code:

out, err = [x.decode("utf-8") for x in  proc.communicate()]

出现错误

UnicodeDecodeError: 'utf8' codec cant decode byte 0xe3 in position 33 ....

实际上,x是包含\xe3(在cp1252中为ã)的字节字符串.因此,我没有使用x.decode('utf-8'),而是使用了x.decode('windows-1252'),这没有给我带来任何错误.为了支持任何一种编码,我最终都使用了x.decode(sys.stdout.encoding). 问题已解决.

Indeed, x was a byte string with \xe3 (which is ã in cp1252) included. So instead of using x.decode('utf-8'), I used x.decode('windows-1252') and that gave me no bugs. To support any kind of encoding, I ended up using x.decode(sys.stdout.encoding) instead. Problem solved.

那是在Windows 7 Starter计算机上的Python 3.2.2中,但是同一台计算机上的Python 2.7也可以正常工作.

And that was in Python 3.2.2 with the Windows 7 Starter computer, but Python 2.7 on the same computer also worked normally.

这篇关于Python Popen无法在Windows PowerShell中使用正确的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆