【Python2.7中文字符编码,使用Unicode时,选择什么编码格式?】教程文章相关的互联网学习教程文章

python-使用unicode粗俗的分数创建紧凑/对人类友好的浮点数【代码】

是否有任何模块(最好是在标准库中)可以将浮点数转换为更人性化的模块?也许它不是更人性化,但至少更紧凑. 例如. 4.625将变成“4?” (用于识别pi的合理精度的布朗尼积分) 这段代码概述是我能想到的最好的代码:import unicodedatadef simplify_float(number):vf = "VULGAR FRACTION "vulgars = {0.125 : unicodedata.lookup(vf + "ONE EIGHTH"),0.2 : unicodedata.lookup(vf + "ONE FIFTH"),0.25 : unicodedata.lookup(vf + "ONE...

python-哈希错误之前必须对Unicode对象进行编码【代码】

处理类似问题的问题:SO 1、SO 2、SO 3. 我尝试了他们的答案,几乎将任何字符串编码为utf-8,但是hmac仍然告诉我编码我的unicoe字符.最大的问题是我什至无法识别令人讨厌的变量.打印输出告诉我它们是字符串还是字节;在前者的情况下,我附加了.encode(),但这没有帮助. 我正在尝试查询GDAX API,同时也是using the code as given on their API page.由于是为Python2.7编写的,因此我认为编码和所有内容可能存在问题,但这对我来说没有任何...

如何在Python 3 CGI中打印unicode字符?【代码】

我正在尝试运行具有一些Unicode字符串的python CGI脚本.它在控制台中工作正常,但是当我在浏览器中运行它时,出现以下错误-UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\u2026’ in position 198: ordinal not in range(128) 我已经尝试了这两个-encode(‘ascii’,’ignore’)unicodedata.normalize(‘NFKD’,var).encode(‘ascii’,’ignore’).strip()var是保存unicode字符串的变量. 现在如何在浏览器中无错误...

Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in posit【图】

今天,在Anaconda prompt启动python遇到了如下错误: UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xaf in position 553: illegal multibyte sequence? 看了看出错跟踪,查看了如下位置: C:\ProgramData\Anaconda3\lib\site.py", line 439 如下图所示,发现是读取history指定的路径的文件出错,于是就通过python看了看 os.path.join(os.path.expanduser(’~’),’.python_history’)的计算所得路径为(注意:???表示电脑...

python-升级Google Application Engine程序以使用unicode

我有一个简单的Google App Engine应用程序,是使用普通字符串编写的.我意识到我想让它处理unicode.这有什么陷阱吗?我正在考虑当前在实时数据库中已经拥有的所有字符串. (来自我不想让他们沮丧的真实用户.)解决方法:Alexander Kojevnikov说:“数据存储在内部将所有字符串保留为unicode.” 换句话说,您的应用程序已经在各处使用unicode.感谢Google员工提供明智的API.无需进一步的工作.

python-‘unicode’对象没有属性’value1’【代码】

如果我在views.py中有对象列表,例如:myobjects=model.objects.all()然后我得到对象的字段的值:times=myobjects.values_list(‘time’, flat=True)然后我想将属性设置为列表时间的每个对象:for mytime in times:mytime.value1=myobjects.filter(time=mytime).values_list(‘value1’,flat=true)mytime.value2=myobjects.filter(time=mytime).values_list(‘value2’,flat=true)并在模板html文件中使用以下属性:{%for mytime in ...

python-解析从xlrd.Book对象中的单元格读取的unicode字符串【代码】

我正在尝试从使用xlrd(实际上是xlsxrd)读取的excel2007单元中解析一些unicode文本.由于某些原因,xlrd在unicode字符串的开头附加了“ text:”,这使我很难键入强制转换.我最终想要颠倒字符串的顺序,因为它是一个名称,并将与其他几个字母顺序地放在字母中.任何帮助将不胜感激,谢谢. 这是我要执行的操作的一个简单示例:>>> import xlrd, xlsxrd >>> book = xlsxrd.open_workbook('C:\\fileDir\\fileName.xlsx') >>> book.sheet_names...

Python:UnicodeEncodeError:’latin-1’编解码器无法在位置编码字符【代码】

我收到此错误:File "run.py", line 37, in <module>print str1 UnicodeEncodeError: 'latin-1' codec can't encode characters in position 24-29: ordinal not in range(256)尝试仅打印一些日语文本时.实际上,字符串看起来像这样:\u5149\u66dc\u65e5\u3067\u30e9\u30c6 \u30d4\u30af\u30b7\u30fc\u4e71\u7372\u884c\u304d\u307e\u3059 \u5e0c\u671b\u8005\u52df\u96c6\u4e2d\u3067\u3059\uff3e\uff3e它来自JSON文件.我该如何打印...

python-使用实体将unicode对象转换为拉丁字符串【代码】

我有一个Unicode对象x = u"a & 日本語: en?i hall?le"并希望将其转换为具有html-entities之类的latin-1字符串"a &amp; : eni hall?le"其背后的原因是,我希望我的用户能够输入unicode数据,但是需要保存数据的旧数据库仅接受latin-1字符串. (不应转换“”,但必须转换其他特殊字符) 知道在这里使用哪个模块吗?我搜索了编码模块,查找了一些编解码器,并尝试了一些unicode对象的方法,但是没有一个明智的解决方案.解决方法:使用unicode....

python-如何解决xml.etree.ElementTree.iterparse()中的Unicode错误?【代码】

我正在使用Python的xml.etree.ElementTree模块的iterparse()方法读取一个巨大的(千兆字节)XML文件.问题是某些XML文件的文本中偶尔会出现Unicode错误(或者至少是Python 3认为是Unicode错误).我的循环是这样设置的:import xml.etree.ElementTree as etreedef foo():# ...f = open(filename, encoding='utf-8')xmlit = iter(etree.iterparse(f, events=('start', 'end')))(event, root) = next(xmlit)for (event, elem) in xmlit: # ...

Python:使用UnicodeWriter将Unicode写入CSV【代码】

Python文档具有以下代码示例,可将unicode写入csv文件.我认为它已经提到了这种方法,因为csv模块无法处理unicode字符串.class UnicodeWriter:"""A CSV writer which will write rows to CSV file "f",which is encoded in the given encoding."""def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):# Redirect output to a queueself.queue = cStringIO.StringIO()self.writer = csv.writer(self.queue, dialect=d...

python-如何将编码的unicode转换为可读的unicode【代码】

这个问题已经在这里有了答案: > How to print/store non-ASCII characters (unicode?) 2个我对这个主题迷失了,我什至不知道该如何正确表达我的问题. 我在python的unicode中有一堆奇怪的转义分隔符.我想使其易于阅读.我认为它必须再次采用unicode格式,因为在ascii中有些字符无法表示.我怎样才能做到这一点?网上几乎有太多东西,例如编码,解码,str,编解码器等,我不确定该...

终端中的Python NLTK雪球阻止程序UnicodeDecodeError,但Eclipse PyDev没有【代码】

我正在使用snowball词干分析器来词干文档中的单词,如下面的代码片段所示.stemmer = EnglishStemmer()# Stem, lowercase, substitute all punctuations, remove stopwords.attribute_names = [stemmer.stem(token.lower()) for token in wordpunct_tokenize(re.sub('[%s]' % re.escape(string.punctuation), '', doc)) if token.lower() not in stopwords.words('english')]在Eclipse中使用PyDev在文档上运行此代码时,没有收到任何错...

使用Python2的正则表达式中的Unicode类【代码】

可能吗? 此代码在Python3中有效:In [1]: import reIn [2]: re.split(r'\W+', 'Les Misérables') Out[2]: ['Les', 'Misérables']但这在Python2中不起作用:In [1]: import reIn [2]: re.split(r'\W+', u'Les Misérables') Out[2]: [u'Les', u'Mis', u'rables']这也不起作用(在具有es_ES.UTF-8语言环境的Linux上测试):In [1]: import localeIn [2]: locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8') Out[2]: 'es_ES.UTF-8'In [3]:...

python-如何避免刮擦UnicodeEncodeError【代码】

我的parse_item回调中有以下代码:sel = Selector(response) item['name'] = sel.xpath('//div[@class="productDescriptionBlock"]/h2/text()').extract()[0] return item但是我得到了UnicodeEncodeError:exceptions.UnicodeEncodeError: 'charmap' codec can't encode character u'\uff01' in position 271761: character maps to <undefined>我也尝试添加.encode(‘utf-8’),但仍然收到相同的错误.Traceback (most recent call ...