如何从unicode字符串编码的inutf-8打印第一个(国家)字符?

你好,
我在塔模板(mako)中的Unicode字符串有问题.我会
在我的字符串中打印第一个字符,在UTF-8和Urllib.Quote()中编码
例如字符串"£ukasz":
$ {urllib.unquote(c.user.firstname).encode('latin-1')[0:1]}
我收到了这些信息:
:'utf8'编解码器无法解码字节
0xc5在位置0:数据的意外结束
当我从[0:1]更改为[0:2]时,一切正常.我觉得是这样的
由于Unicode和编码UTF-8(2个字节).
如何解决这个问题?
此致

# 回答1


在1 WRZ,15:10," Marco Bizzarri" 首先:您是在谈论UTF8编码,但是您已经写了Latin1
编码.即使我不知道mako模板,也应该没有
您的代码段中的问题,如果编码为Latin1,至少是
我能理解的.
不要假设UTF8是两个字节编码; UTF8是可变长度
编码.的确,
编码为utf8的'a"是" a"(一个字节)
'â'编码为utf8是'\ xc3 \ xa0'(两个字节).
您能解释一下要完成的工作吗(而不是如何
您正在尝试实现它)吗?
问候
马可
- -
marco bizzarrihttp://notenotturne.blogspot.com/http://iliveinpisa.blogspot.com/
当我做$ {urllib.unquote(c.user.firstname)}时
Latin-1我的炭是与我所获得的不同的:没有Åukasz,但是âukasz
# 回答2


2008年9月1日星期一,下午3:25, 太疯狂了. "字符串" .encode('latin1')为您提供了一个编码的拉丁语
细绳; Latin1是一个单个字节编码,因此进行第一个字节
字节应该没有问题.
你有没有尝试过:
urlib.unquote(c.user.firstname)[0] .encode('latin1')或
urlib.unquote(c.user.firstname)[0] .encode('utf8')
我在这里假设urlib.unquote(c.user.firstname)返回
编码字符串(我绝对不确定),但是如果这样做,
应该采用第一个"角色".
问候
马可
- -
marco bizzarri http://notenotturne.blogspot.com/ http://iliveinpisa.blogspot.com/(_@_ _)
" marco bizzarri" 新闻:Ma *******************************************************************************************
太疯狂了. "字符串" .encode('latin1')为您提供了一个编码的拉丁语
细绳; Latin1是一个单个字节编码,因此进行第一个字节
字节应该没有问题.
你有没有尝试过:
urlib.unquote(c.user.firstname)[0] .encode('latin1')或
urlib.unquote(c.user.firstname)[0] .encode('utf8')
我在这里假设urlib.unquote(c.user.firstname)返回
编码字符串(我绝对不确定),但是如果这样做,
应该采用第一个"角色".
OP指出,原始字符串是"编码在UTF-8中,
urllib.quote()",因此,在URLLIB.unquote之后,该字符串为UTF-8格式.
在删除第一个之前,必须将其解码为Unicode字符串
特点:
urllib.unquote(c.user.firstname).decode('utf-8')[0]
下一个问题是OP的示例字符串"址"中的字符不是
在拉丁-1中存在 编码,但使用UTF-8编码证明了
收集了完整的两字节UTF-8编码字符:
'%c5%81ukasz'
'\ xc5 \ x81'
-标记
# 回答3


在2 WRZ,06:05," Mark Tolonen" OP指出,原始字符串是"编码在UTF-8中,
urllib.quote()",因此,在URLLIB.unquote之后,该字符串为UTF-8格式.
在删除第一个之前,必须将其解码为Unicode字符串
特点:
urllib.unquote(c.user.firstname).decode('utf-8')[0]
下一个问题是OP的示例字符串"址"中的字符不是
存在于拉丁-1编码中,但是使用UTF-8编码表明
收集了完整的两字节UTF-8编码字符:
'%c5%81ukasz'
'\ xc5 \ x81'
-标记
@mark,当我尝试Urllib.unquote(c.user.firstname)时,.decode('utf-8')
[0] .encode('utf-8'),我收到此消息:
模块塔.插图:344渲染
<< ** cache_args)
return pylons.buffet.render(template_name = template,
片段=片段,
格式=格式,名称空间= kargs,
** cache_args)
模块塔.插图:229渲染
<< log.debug("带有引擎%s的渲染模板%s",
full_path,Engine_name)
返回引擎_config ['Engine'].渲染(名称空间,
模板= full_path,
**选项)> **选项)
Mako.ext.Turbogears:49渲染
<< info.update(self.extra_vars_func())
返回模板.Render(**信息)
模块mako.template:114渲染
<<由此模板的内部渲染方法声明
也从给定的 *args中拉出**数据
成员.成员."""
返回Runtime._render(self,self.callable_,args,data)
def render_unicode(self, *args,**数据):>返回
runtime._render(self,self.callable_,args,data)
mako.runtime:287 in _render
<< context = context(buf,**数据)
context._with_template =模板
_render_context(template,callable_,context, *args, *
** _ kwargs_for_callable(callable_,data))
返回context.pop_buffer().getValue()>>
_render_context(template,callable_,context, *args, *
** _ kwargs_for_callable(callable_,data))
mako.runtime:304 in _render_context
<<#如果主要渲染方法,请从
继承堆栈
(继承,lclcontext)= _populate_self_namespace(上下文,
TMPL)
_exec_template(继承,lclcontext,args = args,
Kwargs = Kwargs)
别的:
#否则,请调用指定的实际渲染方法>>
_exec_template(继承,lclcontext,args = args,kwargs = kwargs)
mako.runtime:337 in _exec_template
<< error_template.render_context(上下文,
错误=错误)
别的:
callable_(上下文, *args,** kwargs)> callable_(上下文,
*args,** kwargs)
模块_reports_create_report_step2_mako:57 in render_body
<<
context.write(filters.decode.utf8(urllib.unquote(s tr(c.period.end))))))))
context.write(u' +')
context.write(filters.decode.utf8(urllib.unquote(c.user.firstname).decode('utf-8')
[0] .encode('utf-8')))))
context.write(filters.decode.utf8(urllib.unqu) ote(s tr(c.user.secondname)
[0:1])))))
context.write(u' \ r \ n name =" works []" value ="')>>
context.write(filters.decode.utf8(urllib.unquote(c.user.firstname).decode('utf-8')
[0] .encode('utf-8')))))
模块Encodings.utf_8:16解码
<<
def解码(输入,错误='strict'):
return codecs.utf_8_decode(输入,错误,true)
class regementalencoder(codecs.incrementalencoder):>返回
codecs.utf_8_decode(输入,错误,true)
<
位置0-1中的字符:序数不在范围内(128)
# 回答4


在2 WRZ,10:17,Sni ...@gmail.com写道:
'%c5%81ukasz'
'\ xc5 \ x81'

@mark,当我尝试Urllib.unquote(c.user.firstname)时,.decode('utf-8')
[0] .encode('utf-8'),我收到此消息:
模块塔.插图:344渲染
<< ** cache_args)
return pylons.buffet.render(template_name = template,
片段=片段,
格式=格式,名称空间= kargs,
** cache_args)
模块塔.插图:229渲染
<< log.debug("带有引擎%s的渲染模板%s",
full_path,Engine_name)
返回引擎_config ['Engine'].渲染(名称空间,
模板= full_path,
**选项)> **选项)
Mako.ext.Turbogears:49渲染
<< info.update(self.extra_vars_func())
返回模板.Render(**信息)
模块mako.template:114渲染
<<由此模板的内部渲染方法声明
也从给定的 *args中拉出**数据
成员.成员."""
返回Runtime._render(self,self.callable_,args,data)
def render_unicode(self, *args,**数据):>返回
runtime._render(self,self.callable_,args,data)
mako.runtime:287 in _render
<< context = context(buf,**数据)
context._with_template =模板
_render_context(template,callable_,context, *args, *
** _ kwargs_for_callable(callable_,data))
返回context.pop_buffer().getValue()>>
_render_context(template,callable_,context, *args, *
** _ kwargs_for_callable(callable_,data))
mako.runtime:304 in _render_context
<<#如果主要渲染方法,请从
继承堆栈
(继承,lclcontext)= _populate_self_namespace(上下文,
TMPL)
_exec_template(继承,lclcontext,args = args,
Kwargs = Kwargs)
别的:
#否则,请调用指定的实际渲染方法>>
_exec_template(继承,lclcontext,args = args,kwargs = kwargs)
mako.runtime:337 in _exec_template
<< error_template.render_context(上下文,
错误=错误)
别的:
callable_(上下文, *args,** kwargs)> callable_(上下文,
*args,** kwargs)
模块_reports_create_report_step2_mako:57 in render_body
<<
context.write(filters.decode.utf8(urllib.unquote(s tr(c.period.end))))))))
context.write(u' +')
context.write(f ilters.decode.utf8(urllib.unquote(c .user.firstname).decode('utf-8') [0] .encode('utf-8'))))) context.write(filters.decode.utf8(urllib.unquote(s tr(c.user.secondname)) [0:1]))))) context.write(u' \ r \ n 标签: python

添加新评论