草庐IT

unicode_literals

全部标签

python - 向后解码 Unicode 文本

许多文本编码都具有这样的特性,即您可以向后遍历编码文本并且仍然能够对其进行解码。ASCII、UTF-8、UTF-16和UTF-32都具有此属性。这让您可以做一些方便的事情,比如读取文件的最后一行而不读取它之前的所有行,或者从文件中的当前位置向后退几行。不幸的是,Python似乎没有提供任何向后解码文件的方法。您不能向后读取,也不能通过编码文件中的字符数量查找。codecs中的解码器模块支持增量解码向前,但不支持向后。似乎没有任何“UTF-8-backwards”编解码器我可以将UTF-8字节以相反的顺序提供给它。我或许可以自己实现依赖于编解码器的字符边界同步,向后读取二进制block,

Python:如何强制 "print"使用 __unicode__ 而不是 __str__,或者自然地使用 "print"消息而不显式调用 unicode()

基本上我只想能够使用一个名为Bottle的类创建实例:例如classBottle(object):...然后在另一个模块中能够简单地“打印”任何实例无需破解代码即可显式调用字符编码例程。总而言之,当我尝试时:obj=Bottle(u"味精")printobj或者“就地”“打印”:printBottle(u"味精")我得到:"UnicodeEncodeError:'ascii'codeccan'tencodecharacters"类似的stackoverflow问题:unicodeclassinPythonhowtoprintchinesewordinmycode..usingpytho

Python Unicode解码错误

我正在编写一个Python程序来读取输出到文本文档中的DOS树命令。当我到达循环的第533次迭代时,Eclipse给出错误:Traceback(mostrecentcalllast):File"E:\Peter\Documents\EclipseWorkspace\MusicManagement\InputTest.py",line24,ininput=myfile.readline()File"C:\Python33\lib\encodings\cp1252.py",line23,indecodereturncodecs.charmap_decode(input,self.error

python - 使用 Unicode 将 CSV 转换为 YAML?

我正在尝试使用Python3.4将包含Unicode字符串的CSV文件转换为YAML文件。目前,YAML解析器将我的Unicode文本转义为ASCII字符串。我希望YAML解析器将Unicode字符串导出为Unicode字符串,不带转义字符。当然,我在这里误解了一些东西,非常感谢任何帮助。奖励积分:如何使用Python2.7完成此操作?CSV输入id,title_english,title_russian1,ATitleinEnglish,Названиенарусском2,AnotherTitle,ДругойНазвание当前YAML输出-id:1title_english:A

python长度的unicode字符串混淆

已经有一些帮助解决了这个问题,但我仍然很困惑。我有一个像这样的unicode字符串:title=u'?test'title_length=len(title)#5但是!我需要len(title)为6。客户希望它为6,因为他们的计算方式似乎与我在后端的计算方式不同。作为一种解决方法,我编写了这个小helper,但我确信它可以改进(有足够的编码知识)或者它甚至可能是错误的。title_length=len(title)+repr(title).count('\\U')#61。有没有更好的方法让长度为6?:-)我假设我(Python)正在计算unicode字符的数量,即5。客户端正在计算字节

python - 我可以在主导入文件中使用 "from __future__ import unicode_literals"吗?

我正在创建一些必须在2.6、2.7和3.3下运行的演示Python脚本。作为其中的一部分,每个模块都带有前缀from__future__importunicode_literals是否可以将该指令从每个模块中剪切下来并粘贴到一个通用的导入文件中?例如#master.pyfrom__future__importunicode_literals#file1.pyimportmaster#file2.pyimportmaster 最佳答案 没有。引用文档:Afuturestatementisadirectivetothecompilert

python - Unicode解码错误: 'ascii' codec can't decode byte 0xc5

UnicodeDecodeError:'ascii'codeccan'tdecodebyte0xc5inposition537:ordinalnotinrange(128),referer:...当我尝试用字符“č”输出我的整个网站时,我总是会遇到这个错误。我正在使用mako模板。怎么办? 最佳答案 发生错误是因为某处代码将您的unicode模板字符串强制转换为python2str;您需要自己将呈现的模板编码为UTF-8字节串:ifisinstance(rendered,unicode):rendered=rendered.enco

python 3 : How do I get a string literal representation of a byte string?

在Python3中,如何将字节字符串插入到常规字符串中并获得与Python2相同的行为(即:仅获取没有b前缀或双反斜杠的转义码)?例如:python2.7:>>>x=u'\u041c\u0438\u0440'.encode('utf-8')>>>str(x)'\xd0\x9c\xd0\xb8\xd1\x80'>>>'x=%s'%x'x=\xd0\x9c\xd0\xb8\xd1\x80'python3.3:>>>x=u'\u041c\u0438\u0440'.encode('utf-8')>>>str(x)"b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'">>>'x=

python - 关于 unicode 和 utf-8 编码,python 中的 `%` 格式运算符和 `str.format()` 之间有区别吗?

假设n=u"Tübingen"repr(n)#`T\xfcbingen`#Unicodei=1#integer以下文件中的第一个抛出UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xfc'inposition82:ordinalnotinrange(128)当我执行n.encode('utf8')时,它会起作用。第二个在这两种情况下都完美无缺。#PythonFile1##!/usr/bin/envpython-B#encoding:utf-8print'{id},{name}'.format(id=i,name=n)#Pyth

python 2.X : Why Can't I Properly Handle Unicode?

我已经尝试了一段时间Python2.X和unicode。但我已经到了没有意义的地步。第一个问题:一些代码会清楚地解释我的意思。txt变量在这里模拟pyqt4的翻译功能。它返回一个QString。#-*-coding:utf-8-*-fromPyQt4importQtCoretxt=QtCore.QString(u'puòessere/sarà/日本語')txtUnicode1=unicode(txt,errors='replace')txtUnicode2=unicode(txt)当print()-ing两个unicode字符串时,我得到:pu�essere/sar�/???puòes