微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – UnicodeEncodeError:’ascii’编解码器无法编码字符?

我试图通过正则表达式传递大量的随机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)

完整脚本:

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()
你的另一个问题 here的总重复次数(虽然在这里你最终设法从头开始向我们展示CODE!哇! – ).答案仍然相同:而不是
ofile.write(Test + '\n')

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

为什么你一直在重复这个Q,BTW?!

原文地址:https://www.jb51.cc/regex/357310.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐