我经常使用mako得到这个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 6: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我告诉mako我正在以任何可能的方式使用unicode:
mylookup = TemplateLookup(
directories=['plugins/stl/templates'],
input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'],
encoding_errors='replace')
self.template = Template(self.getTemplate(), lookup=mylookup,
module_directory=tempfile.gettempdir(),
input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'],
encoding_errors='replace')
html = self.template.render_unicode(data=self.stuff)
Run Code Online (Sandbox Code Playgroud)
我的所有模板文件都以:
## -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
并且,在它们内部,所有costant字符串都以"u"为前缀.我知道self.stuff参数包含unicode字符串,但我实例化mako对象的方式应该处理它(否则这些参数有什么用处?).有什么我忘了做的吗?
还有一个问题:encoding_errors ='replace'有什么意义?
= EDIT =我只留下一个unicode字符串,这是回溯:
Traceback (most recent call last):
File "C:\My Dropbox\src\flucso\src\plugins\stl\main.py", line 240, in updateView
flags=self.makoflags)
File "C:\Python26\lib\site-packages\mako-0.3.4-py2.6.egg\mako\template.py", line 198, in render_unicode
as_unicode=True)
File "C:\Python26\lib\site-packages\mako-0.3.4-py2.6.egg\mako\runtime.py", line 403, in _render
_render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data)) …Run Code Online (Sandbox Code Playgroud) 我有一些像这样的XML结构:
var struct:XML = <mh>
<mi id="1" stuff="whatever"/>
<mi id="2" stuff="whatever"/>
<mi id="3" stuff="whatever"/>
</mh>;
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过"id"访问子节点,这样:
var stuff:Object = struct.(hasOwnProperty('@id') && @id == '2').@stuff;
Run Code Online (Sandbox Code Playgroud)
现在我有一些类似的ArrayCollection结构:
private var cmenu:ArrayCollection = new ArrayCollection([
{id:"1", stuff:"whatever"},
{id:"2", stuff:"whatever"},
{id:"3", stuff:"whatever"}
]);
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以以类似的方式访问项目,如下所示:
var stuff:Object = cmenu['id == 2'].stuff;
Run Code Online (Sandbox Code Playgroud)
可能吗?
我想这应该是一个简单的任务:更改 QWebView 的内容(它总是包含几页内容),然后将页面滚动回之前的位置:
y = self.webView.page().mainFrame().scrollPosition().y()
self.webView.setHtml(looong_html_text)
if y != 0:
self.webView.scroll(0, y)
self.webView.page().mainFrame().scroll(0, y)
self.webView.page().mainFrame().setScrollPosition(QPoint(0, y))
print(self.webView.page().mainFrame().scrollPosition().y())
Run Code Online (Sandbox Code Playgroud)
但是 if 里面的 3 个命令完全没用:页面滚动回到顶部。怎么了?
我试图在python中从这个线程翻译代码:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
__data__ = [
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt …Run Code Online (Sandbox Code Playgroud) 如您所知,格式如下:
"YYYY-MM-DDTHH:NN:SS.ZZZ+XX:XX" (i.e. "2009-03-24T16:24:32.057+01:00")
Run Code Online (Sandbox Code Playgroud)
我必须在 ActionScript3 类中完成,但任何来源都将不胜感激,谢谢。