我从Python2.6的subprocess模块中调用pipe.communicate。我从这段代码中得到以下错误:
from subprocess import Popen
pipe = Popen(cwd)
pipe.communicate( data )对于任意的cwd,其中包含unicode的data (特别是0xE9):
Exec. exception: 'ascii' codec can't encode character u'\xe9' in position 507: ordinal not in range(128)
Traceback (most recent call last):
... stdout, stderr = pipe.communicate( data )
File
"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py",
line 671, in communicate
return self._communicate(input)
File
"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py",
line 1177, in _communicate
bytes_written = os.write(self.stdin.fileno(), chunk)我猜想,之所以会发生这种情况,是因为pipe.communicate()期望使用ASCII码编码的字符串,而data是unicode。
这就是我遇到的问题吗?我找到了一种将unicode传递给pipe.communicate()的方法
感谢您的阅读!
布赖恩
发布于 2010-06-15 03:08:04
我可能已经解决了这个问题,通过改变:
pipe.communicate( data )至
pipe.communicate( data.encode('utf8') )尽管我会被纠正!
布赖恩
https://stackoverflow.com/questions/3040101
复制相似问题