我在处理从不同网页(在不同网站上)获取的文本中的unicode字符时遇到问题.我正在使用BeautifulSoup.
问题是错误并不总是可重现的; 它有时适用于某些页面,有时候,它会通过抛出一个UnicodeEncodeError.我已经尝试了几乎所有我能想到的东西,但是我没有找到任何可以持续工作的东西而不会抛出某种与Unicode相关的错误.
导致问题的代码部分之一如下所示:
agent_telno = agent.find('div', 'agent_contact_number')
agent_telno = '' if agent_telno is None else agent_telno.contents[0]
p.agent_info = str(agent_contact + ' ' + agent_telno).strip()
Run Code Online (Sandbox Code Playgroud)
以下是运行上述代码段时在SOME字符串上生成的堆栈跟踪:
Traceback (most recent call last):
File "foobar.py", line 792, in <module>
p.agent_info = str(agent_contact + ' ' + agent_telno).strip()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我怀疑这是因为某些页面(或更具体地说,来自某些站点的页面)可能被编码,而其他页面可能是未编码的.所有这些网站都位于英国,并提供供英国消费的数据 - 因此,没有与内部化或处理用英语以外的任何文字处理的文本相关的问题.
有没有人有任何想法如何解决这个问题,以便我可以一致地解决这个问题?
说我有一个功能:
def NewFunction():
return '£'
Run Code Online (Sandbox Code Playgroud)
我想在它前面打一些带有井号的东西,当我尝试运行这个程序时它会输出错误,显示以下错误信息:
SyntaxError: Non-ASCII character '\xa3' in file 'blah' but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何在返回功能中加入英镑符号?我基本上是在课堂上使用它,它'__str__'包含在包含英镑符号的部分内.
我有一个套接字服务器,应该从客户端接收UTF-8有效字符.
问题是一些客户端(主要是黑客)正在通过它发送所有错误的数据.
我可以很容易地区分真正的客户端,但我正在记录所有发送的数据文件,以便我以后可以分析它.
有时我会得到这样的字符œ导致UnicodeDecodeError错误.
我需要能够使用或不使用这些字符来生成字符串UTF-8.
更新:
对于我的特殊情况,套接字服务是一个MTA,因此我只希望接收ASCII命令,如:
EHLO example.com
MAIL FROM: <john.doe@example.com>
...
Run Code Online (Sandbox Code Playgroud)
我用JSON记录了所有这些.
然后一些没有良好意图的人决定出售各种垃圾.
这就是为什么对于我的特定情况,剥离非ASCII字符是完全可以的.
我真的很困惑.我试着编码,但错误说can't decode....
>>> "??".encode("utf8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我知道如何避免字符串上带有"u"前缀的错误.我只是想知道为什么在调用编码时错误是"无法解码".什么是Python在幕后做什么?
我想写一本字典,英文单词指向俄语和法语翻译.
如何在Python中打印出unicode字符?另外,如何在变量中存储unicode字符?
可能重复:
如何将ASCII字符串视为unicode并在python中对其中的转义字符进行转换?
如何将unicode转义序列转换为python字符串中的unicode字符
我有一个字符串,其中包含unicode字符,例如\u2026等等.不知何故,我没有收到它unicode,但是收到的是str.如何将其转换回unicode?
>>> a="Hello\u2026"
>>> b=u"Hello\u2026"
>>> print a
Hello\u2026
>>> print b
Hello…
>>> print unicode(a)
Hello\u2026
>>>
Run Code Online (Sandbox Code Playgroud)
所以显然unicode(a)不是答案.那是什么?
我试图运行这段代码,并不断给出错误说"没有编码的字符串参数"
ota_packet = ota_packet.encode('utf-8') + bytearray(content[current_pos:(final_pos)]) + '\0'.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我在python2.7中有一个像这样的字符串,
This is some \u03c0 text that has to be cleaned\u2026! it\u0027s annoying!
Run Code Online (Sandbox Code Playgroud)
我怎么把它转换成这个,
This is some text that has to be cleaned! its annoying!
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
printinfo = title + "\t" + old_vendor_id + "\t" + apple_id + '\n'
# Write file
f.write (printinfo + '\n')
Run Code Online (Sandbox Code Playgroud)
但是我在运行时遇到这个错误:
f.write(printinfo + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
它正在编写这个:
Identité secrète (Abduction) [VF]
Run Code Online (Sandbox Code Playgroud)
请任何想法,不知道如何解决.
干杯.
更新:这是我的大部分代码,所以你可以看到我在做什么:
def runLookupEdit(self, event):
newpath1 = pathindir + "/"
errorFileOut = newpath1 + "REPORT.csv"
f = open(errorFileOut, 'w')
global old_vendor_id
for old_vendor_id in vendorIdsIn.splitlines():
writeErrorFile = 0
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True) …Run Code Online (Sandbox Code Playgroud) 我正在使用Google App Engine开发一个小应用程序,该应用程序使用Quora RSS提要.有一个表单,根据用户输入的输入,它将输出与输入相关的链接列表.现在,应用程序适用于单个字母查询和大多数双字母单词,如果单词用' - '分隔.但是,对于三个字母的单词和一些双字母单词,我收到以下错误:
UnicodeDecodeError:'ascii'编解码器无法解码位置48中的字节0xe2:序数不在范围内(128)
这是我的Python代码:
import os
import webapp2
import jinja2
from google.appengine.ext import db
import urllib2
import re
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainPage(Handler):
def get(self):
self.render("formrss.html")
def post(self):
x = self.request.get("rssquery")
url = "http://www.quora.com/" + x + "/rss"
content = urllib2.urlopen(url).read()
allTitles = re.compile('<title>(.*?)</title>')
allLinks = …Run Code Online (Sandbox Code Playgroud) python ×10
python-unicode ×10
unicode ×6
python-2.x ×3
encoding ×2
python-2.7 ×2
jinja2 ×1
linux ×1
python-3.x ×1
string ×1
xml ×1