我想通过正则表达式通过随机HTML的大串和我的Python 2.6的脚本哽咽这样的:

UnicodeEncodeError: '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(Whois + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 1005: ordinal not in range(128)

完整脚本:

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 spamReader:
            sel.open(row[0])
            time.sleep(10)
            Test = sel.get_text("//html/body/div/table/tbody/tr/td/form/div/table/tbody/tr[7]/td")
            Test = Test.replace(",","")
            Test = Test.replace("\n", "")
            ofile = open('TestOut.csv', 'ab')
            ofile.write(Test + '\n')
            ofile.close()

    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
有帮助吗?

解决方案

您正在尝试一个字节字符串传递到的东西,但它是不可能的(从信息您提供的稀缺性)来告诉什么你想通过它来。开始时你不能编码为ASCII(默认编解码器)Unicode字符串,那么,你就会有一些不同的编解码器编码(或音译它,@ R.Pate建议) - 但它使用到是不可能的说的什么的编解码器,你应该使用,因为我们不知道你传递什么字节字符串,因此不知道这是什么未知的子系统是要能够在以下方面正确地接受和处理编解码器。

在你在离开我们这样完全黑暗的环境,utf-8是一个合理的盲目猜测(因为它是能够代表任何Unicode字符串完全作为一个字节串编解码器,它的多种用途,如XML标准的编解码器) - 但它不能有任何超过一个盲目的猜测,直到除非你要告诉我们更多关于 你想要什么,传递字节串来,为了什么目的。

传递thestring.encode('utf-8')而不是裸thestring肯定会避免你现在所看到的特定错误,但它可能会导致特殊的显示器(或不管它的您正试图与其他字节字符串做!),除非收件人是准备,愿意并且能够接受UTF-8编码(以及我们如何能知道,有什么收件人也可能会被完全为零的想法 - ?!)

其他提示

您正试图转换的Unicode中的 “严格” 的模式为ASCII模式:

>>> help(str.encode)
Help on method_descriptor:

encode(...)
    S.encode([encoding[,errors]]) -> object

    Encodes S using the codec registered for encoding. encoding defaults
    to the default encoding. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
    'xmlcharrefreplace' as well as any other name registered with
    codecs.register_error that is able to handle UnicodeEncodeErrors.

您可能需要类似以下内容之一:

s = u'Protection™'

print s.encode('ascii', 'ignore')    # removes the ™
print s.encode('ascii', 'replace')   # replaces with ?
print s.encode('ascii','xmlcharrefreplace') # turn into xml entities
print s.encode('ascii', 'strict')    # throw UnicodeEncodeErrors

“最好”的方式总是取决于您的要求;所以,你的是什么?无视非ASCII合适?如果您更换™与 “(TM)”? (看起来看中了这个例子,但很快就分解为其他码点,但它可能就是你想要的。)可能例外是你所需要的东西;现在你只需要处理它以某种方式?

只有你才能真正回答这个问题。

首先,尝试英文翻译安装(或任何其他如果需要):

sudo apt-get install language-pack-en

它提供了所有受支持的包(包括Python)。

翻译数据更新

和确保使用正确的编码在代码中。

例如:

open(foo, encoding='utf-8')

然后仔细检查系统配置等LANG或区域设置(/etc/default/locale)的配置的值,不要忘记重新登录会话。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top