管道Python程序的输出时,Python解释器会对编码感到困惑,并将其设置为None.这意味着这样的程序:
# -*- coding: utf-8 -*-
print u"åäö"
Run Code Online (Sandbox Code Playgroud)
正常运行时会正常工作,但失败时:
UnicodeEncodeError:'ascii'编解码器无法对位置0中的字符u'\ xa0'进行编码:序数不在范围内(128)
当在管道序列中使用时.
在配管时使这项工作的最佳方法是什么?我可以告诉它使用shell/filesystem /无论使用什么编码吗?
到目前为止我看到的建议是直接修改你的site.py,或者使用这个hack对defaultencoding进行硬编码:
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print u"åäö"
Run Code Online (Sandbox Code Playgroud)
是否有更好的方法使管道工作?
当我尝试在Windows控制台中打印Unicode字符串时,出现UnicodeEncodeError: 'charmap' codec can't encode character ....错误.我认为这是因为Windows控制台不接受仅Unicode字符.最好的方法是什么??在这种情况下,有什么方法可以让Python自动打印而不是失败?
编辑: 我正在使用Python 2.5.
注意: @ LasseV.Karlsen回答带有复选标记有点过时(从2008年开始).请谨慎使用下面的解决方案/答案/建议!!
截至今天(2016年1月6日),@ JFSebastian答案更为相关.
我想解析我的XML文档.所以我存储了我的XML文档,如下所示
class XMLdocs(db.Expando):
id = db.IntegerProperty()
name=db.StringProperty()
content=db.BlobProperty()
Run Code Online (Sandbox Code Playgroud)
现在我的下面是我的代码
parser = make_parser()
curHandler = BasketBallHandler()
parser.setContentHandler(curHandler)
for q in XMLdocs.all():
parser.parse(StringIO.StringIO(q.content))
Run Code Online (Sandbox Code Playgroud)
我收到了以下错误
'ascii' codec can't encode character u'\xef' in position 0: ordinal not in range(128)
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 517, in __call__
handler.post(*groups)
File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/base_handler.py", line 59, in post
self.handle()
File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py", line 168, in handle
scan_aborted = not self.process_entity(entity, ctx)
File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py", line 233, in process_entity
handler(entity)
File "/base/data/home/apps/parsepython/1.348669006354245654/parseXML.py", line 71, in process
parser.parse(StringIO.StringIO(q.content))
File …Run Code Online (Sandbox Code Playgroud) 我试图通过正则表达式传递大量随机html,我的Python 2.6脚本对此感到窒息:
UnicodeEncodeError:'ascii'编解码器无法编码字符
我追溯到这个词末尾的商标上标:Protection™ - 我希望将来会遇到其他类似的人.
是否有处理非ascii字符的模块?或者,在python中处理/转义非ascii内容的最佳方法是什么?
谢谢!完整错误:
E
======================================================================
ERROR: test_untitled (__main__.Untitled)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\Test2.py", line 26, in test_untitled
ofile.write(Whois + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 1005: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
完整脚本:
from selenium import selenium
import unittest, time, re, csv, logging
class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://www.BaseDomain.com/")
self.selenium.start()
self.selenium.set_timeout("90000")
def test_untitled(self):
sel = self.selenium
spamReader = csv.reader(open('SubDomainList.csv', 'rb'))
for row in …Run Code Online (Sandbox Code Playgroud) 我试图通过正则表达式传递大量随机html,我的Python 2.6脚本对此感到窒息:
UnicodeEncodeError:'ascii'编解码器无法编码字符
我在这个词的末尾追溯到商标上标:Protection™ - 我不需要捕获非ascii的东西,但这是一个令人讨厌的东西,我希望将来会更多地遇到它.
是否有处理非ascii字符的模块?或者,在python中处理/转义非ascii内容的最佳方法是什么?
谢谢!完整错误:
E
======================================================================
ERROR: test_untitled (__main__.Untitled)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python26\Test2.py", line 26, in test_untitled
ofile.write(Test + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 1005: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
完整脚本:
from selenium import selenium
import unittest, time, re, csv, logging
class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://www.BaseDomain.com/")
self.selenium.start()
self.selenium.set_timeout("90000")
def test_untitled(self):
sel = self.selenium
spamReader = csv.reader(open('SubDomainList.csv', 'rb'))
for row in …Run Code Online (Sandbox Code Playgroud) 我已经尝试了所有可以找到的解决方案,但似乎没有任何效果:
teext = str(self.tableWidget.item(row, col).text())
Run Code Online (Sandbox Code Playgroud)
我顺便写一下希腊文...
python ×4
python-2.6 ×2
regex ×2
unicode ×2
ascii ×1
encode ×1
encoding ×1
pyqt ×1
python-2.x ×1
stdout ×1
terminal ×1
xml-parsing ×1