UnicodeEncodeError:“ASCII”编解码器不能编码字符?(UnicodeEncod

2019-09-03 00:44发布

我试图通过正则表达式和我的Python 2.6的脚本窒息对这个通过随机HTML的大字符串:

UnicodeEncodeError:“ASCII”编解码器不能编码字符

我追溯回这个单词的末尾的商标上标:保护™ - 我不需要捕捉非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)

完整的脚本:

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()

Answer 1:

您的其他问题的共重复这里 (虽然这里你终于屈尊向我们展示了从起始码,哇- !)。 答案仍然是相同的:不是

        ofile.write(Test + '\n')

        ofile.write(Test.encode('utf8') + '\n')

你为什么不停地重复这个Q,顺便说一句?



文章来源: UnicodeEncodeError: 'ascii' codec can't encode character?