【问题标题】:UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 619: ordinal not in range(128)UnicodeEncodeError:'ascii' 编解码器无法在位置 619 编码字符 u'\xa3':序数不在范围内(128)
【发布时间】:2026-02-19 01:55:02
【问题描述】:

我遇到了臭名昭著的UnicodeEncodeError。在发布此错误之前,我已经对此错误进行了很多研究,显然此错误有多个版本。

这是我的代码:

import nltk,re,pprint
import geograpy
import codecs
from nameparser.parser import HumanName

text_file = codecs.open('H:/Study and Work/Marciano Asstship/Work/Incident_cards_data/2.Text Extracted/Boxes 1 - 21/Boxes 8 thru 21-TULE LAKE/Box9-TULE LAKE/box9.txt',encoding = 'utf-8')
text_data = text_file.read()

places = geograpy.get_place_context(text_data)
print places

这是错误:

log.debug('%s on %s' % (e, url))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 619: ordinal not in range(128)

【问题讨论】:

  • 是什么让您的情况与对这个确切问题的众多个其他答案截然不同?这些其他解决方案在哪些方面无法解决您的问题?
  • 在所有其他情况下,使用 'utf-8' 编码可以解决问题。在我的情况下,如果我使用 utf-8 编码,则会收到上述错误,如果我使用 utf-8 解码,则会收到 UnicodeDecodeError。我也尝试了其他几种编码,但似乎都没有。

标签: python unicode utf-8 nltk


【解决方案1】:

eurl 是一个 Unicode 字符串,这使得传递给 log.debug 的结果字符串成为一个 Unicode 字符串。例子:

>>> '%s %s' % ('abc','def')
'abc def'
>>> '%s %s' % (u'abc','def')
u'abc def'
>>> '%s %s' % ('abc',u'def')
u'abc def'
>>> '%s %s' % (u'abc',u'def')
u'abc def'

log.debug 可能不需要 Unicode 字符串,因此 Python 2 使用默认的 ascii 编解码器将其隐式转换为字节字符串。生成的 Unicode 字符串与 ASCII 不兼容,因此是 UnicodeEncodeError

【讨论】: