我在处理从不同网页(在不同网站上)获取的文本中的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)
我怀疑这是因为某些页面(或更具体地说,来自某些站点的页面)可能被编码,而其他页面可能是未编码的.所有这些网站都位于英国,并提供供英国消费的数据 - 因此,没有与内部化或处理用英语以外的任何文字处理的文本相关的问题.
有没有人有任何想法如何解决这个问题,以便我可以一致地解决这个问题?
我在Python中有一个Unicode字符串,我想删除所有的重音符号(变音符号).
我在Web上发现了一种在Java中执行此操作的优雅方法:
我是否需要安装pyICU等库?或者只使用python标准库?那python 3怎么样?
重要说明:我想避免代码使用重音字符到非重音符号的显式映射.
我见过很少的py脚本在脚本的顶部使用它.在什么情况下应该使用它?
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
Run Code Online (Sandbox Code Playgroud) 在Python 3.x中,super()可以不带参数调用:
class A(object):
def x(self):
print("Hey now")
class B(A):
def x(self):
super().x()
Run Code Online (Sandbox Code Playgroud)
>>> B().x()
Hey now
Run Code Online (Sandbox Code Playgroud)
为了使这项工作,一些编译时间魔法进行,其中的一个后果是,下面的代码(重新绑定super到super_)失败:
super_ = super
class A(object):
def x(self):
print("No flipping")
class B(A):
def x(self):
super_().x()
Run Code Online (Sandbox Code Playgroud)
>>> B().x()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in x
RuntimeError: super(): __class__ cell not found
Run Code Online (Sandbox Code Playgroud)
super()如果没有编译器的帮助,为什么无法在运行时解析超类?是否存在这种行为或其潜在原因可能会让一个不知情的程序员陷入困境的实际情况?
...并且,作为一个附带问题:在Python中是否有任何其他函数,方法等示例可以通过将它们重新绑定到不同的名称来打破?
我的python(ver 2.7)脚本运行良好,从本地html文件中获取一些公司名称,但是当涉及到某个特定的国家/地区名称时,它会出现此错误"UnicodeEncodeError:'ascii'compodec无法编码字符"
当这个公司名称出现时特别收到错误
公司名称:KühlfixKälteanlagenIng.GerhardDoczekal&Co.KG
该链接无法处理
Traceback (most recent call last):
File "C:\Python27\Process2.py", line 261, in <module>
flog.write("\nCompany Name: "+str(pCompanyName))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
错误在这行代码中给出:
if companyAlreadyKnown == 0:
for hit in soup2.findAll("h1"):
print "Company Name: "+hit.text
pCompanyName = hit.text
flog.write("\nCompany Name: "+str(pCompanyName))
companyObj.setCompanyName(pCompanyName)
Run Code Online (Sandbox Code Playgroud) 我正在使用Flask和Google App Engine构建Web应用程序.此网络应用程序中的一个页面通过YouTube API拨打电话,以获取带有搜索字词的视频.
我尝试查询时收到以下错误YoutubeVids.html.
这只发生在我通过Jinja2模板将某个参数传递给页面时.
file "/Users/xxxxx/App-Engine/src/templates/YoutubeVids.html", line 1, in top-level template code
{% extends "master.html" %}
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
INFO 2014-01-27 22:39:40,963 module.py:612] default: "GET /xxx/yyyy HTTP/1.1" 500 291
Run Code Online (Sandbox Code Playgroud) 我试图将整个段落输入到我的文字处理器中,先将其分成句子然后再分成单词.
我尝试了以下代码,但它不起作用,
#text is the paragraph input
sent_text = sent_tokenize(text)
tokenized_text = word_tokenize(sent_text.split)
tagged = nltk.pos_tag(tokenized_text)
print(tagged)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,给我错误.那么如何将段落标记为句子然后单词呢?
一个示例段落:
这件事似乎压倒了这只小黑褐色的狗,使他受伤了.他在孩子脚下绝望地沉了下去.当重复一击,伴随着幼稚的句子中的警告,他转过身来,以一种特殊的方式握住他的爪子.在他的耳朵和眼睛的同时,他向孩子祈祷.
**警告:**这只是来自互联网的随机文本,我不拥有上述内容.
我在使用Jinja2渲染模板时尝试使用utf-8字符.以下是我的模板的样子:
<!DOCTYPE HTML>
<html manifest="" lang="en-US">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
...
Run Code Online (Sandbox Code Playgroud)
标题变量设置如下:
index_variables = {'title':''}
index_variables['title'] = myvar.encode("utf8")
template = env.get_template('index.html')
index_file = open(preview_root + "/" + "index.html", "w")
index_file.write(
template.render(index_variables)
)
index_file.close()
Run Code Online (Sandbox Code Playgroud)
现在,问题是myvar是从消息队列中读取的消息,可以包含那些特殊的utf8字符(例如"SéptimoCine").
渲染的模板看起来像:
...
<title>S\u00e9ptimo Cine</title>
...
Run Code Online (Sandbox Code Playgroud)
我希望它是:
...
<title>Séptimo Cine</title>
...
Run Code Online (Sandbox Code Playgroud)
我已经做了几次测试,但我不能让它工作.
我试图设置title变量而没有.encode("utf8"),但它抛出异常(ValueError:期望一个字节对象,而不是unicode对象),所以我的猜测是初始消息是unicode
我使用chardet.detect来获取消息的编码(它是"ascii"),然后执行以下操作:myvar.decode("ascii").encode("cp852"),但标题仍未正确呈现.
我还确保我的模板是UTF-8文件,但它没有什么区别.
关于如何做到这一点的任何想法?
您好,我正在尝试创建一个简单的Web应用程序,按照教程计算一个阻尼振动方程,并在将其转换为Base64字符串后将结果的png返回到html页面.
应用程序正常运行,但计算结果时,会返回损坏的图像图标,可能是因为Base64字符串无效.
我已使用在线转换器将另一个png图像转换为Base64字符串,并用于<img src="data:image/png;base64, BASE64_STRING"/>成功显示图像.我相信模板格式正确.我也在这里和这里阅读了其他的SO答案,并尝试实施那些没有成功的答案.
这是返回图像字符串的位置
from numpy import exp, cos, linspace
import matplotlib.pyplot as plt
def damped_vibrations(t, A, b, w):
return A*exp(-b*t)*cos(w*t)
def compute(A, b, w, T, resolution=500):
"""Return filename of plot of the damped_vibration function."""
t = linspace(0, T, resolution+1)
u = damped_vibrations(t, A, b, w)
plt.figure() # needed to avoid adding curves in plot
plt.plot(t, u)
plt.title('A=%g, b=%g, w=%g' % (A, b, w))
from io import BytesIO …Run Code Online (Sandbox Code Playgroud) python ×8
unicode ×4
python-2.x ×3
utf-8 ×3
encoding ×2
flask ×2
python-3.x ×2
api ×1
ascii ×1
base64 ×1
diacritics ×1
html ×1
jinja2 ×1
matplotlib ×1
nltk ×1
python-2.7 ×1
super ×1
sys ×1
youtube ×1