草庐IT

utf8_unicode_cs

全部标签

Python - 有没有办法让项目中的所有字符串默认为 unicode?

而不是在每个字符串前输入u?...以及一些让stackoverflow开心的文字 最佳答案 是的,使用from__future__importunicode_literals>>>from__future__importunicode_literals>>>s='hi'>>>type(s)在Python3中,字符串默认是unicode字符串。 关于Python-有没有办法让项目中的所有字符串默认为unicode?,我们在StackOverflow上找到一个类似的问题:

python - __unicode__() 不返回字符串

我在python中有以下类classmyTest:def__init__(self,str):self.str=strdef__unicode__(self):returnself.str并在其他文件中实例化myTest以试用unicode()方法importmyClassc=myClass.myTest("helloworld")printc打印出来我得到但是,如果我覆盖__str__()我会得到helloworld作为输出。我的问题是,如果我希望它输出字符串,我应该如何为__unicode__()编写覆盖程序? 最佳答案 一般是

python - 如何将 numpy 对象数组转换为 str/unicode 数组?

更新:在最新版本的numpy(例如v1.8.1)中,这不再是问题。此处提到的所有方法现在都正常工作。原问题:有时使用对象dtype存储字符串数组很方便,尤其是当需要修改大数组的内容而无需事先了解字符串的最大长度时,例如,>>>importnumpyasnp>>>a=np.array([u'abc',u'12345'],dtype=object)在某些时候,可能想要将dtype转换回unicode或str。然而,简单的转换将截断长度为4或1的字符串(为什么?),例如>>>b=np.array(a,dtype=unicode)>>>barray([u'abc',u'1234'],dtype

对于单个 Unicode 字符串,Python 返回长度为 2

在Python2.7中:In[2]:utf8_str='\xf0\x9f\x91\x8d'In[3]:print(utf8_str)?In[4]:unicode_str=utf8_str.decode('utf-8')In[5]:print(unicode_str)?In[6]:unicode_strOut[6]:u'\U0001f44d'In[7]:len(unicode_str)Out[7]:2既然unicode_str只包含一个unicode代码点(0x0001f44d),为什么len(unicode_str)返回2而不是1? 最佳答案

Python popen() - 通信(str.encode(编码 ="utf-8",错误 ="ignore"))崩溃

在Windows上使用Python3.4.3。我的脚本在控制台中运行一个小的java程序,应该得到输出:importsubprocessp1=subprocess.Popen([...],stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)out,err=p1.communicate(str.encode("utf-8"))这导致一个正常的'UnicodeDecodeError:'charmap'codeccan'tdecodebyte0x9dinposition135:charactermapst

python - 如何让 PyC​​harm 在其控制台中显示 unicode 数据?

我已经切换到PyCharm并且非常喜欢使用它。我为使用英语以外的语言(即希伯来语和阿拉伯语)的项目编码,并且需要偶尔调试编码。由于某些原因,PyCharm不会在其调试控制台中显示Unicode字符。我已将IDE编码设置为UTF-8,但没有帮助。有什么想法吗? 最佳答案 已接受的答案不再正确。在默认字体中,它们都没有区别。我只是花了一段时间来解决同样的问题,最好的解决方案是修改您的.bash_profile(或.zshrc)并包含以下行:exportPYTHONIOENCODING=UTF-8理论上,您还可以将其添加到您的环境变量中,

python - 使用 Django 将 Unicode 字符存储到 MySQL 时出现问题

我有字符串u"PlayedMirror'sEdge\u2122"应该显示为PlayedMirror'sEdge™但这是另一个问题。我手头的问题是我将其放入模型中,然后尝试将其保存到数据库中。又名:a=models.Achievement(name=u"PlayedMirror'sEdge\u2122")a.save()我得到了:'ascii'codeccan'tencodecharacteru'\u2122'inposition13:ordinalnotinrange(128)完整的堆栈跟踪(根据要求):Traceback:File"/var/home/ptarjan/django/m

Python - 读取奇怪的 utf-16 格式的文本文件

我正在尝试将文本文件读入python,但它似乎使用了一些非常奇怪的编码。我像往常一样尝试:file=open('data.txt','r')lines=file.readlines()forlineinlines[0:1]:printline,printline.split()输出:0.02001971.97691e-005['0\x00.\x000\x002\x000\x000\x001\x009\x007\x00','\x001\x00.\x009\x007\x006\x009\x001\x00e\x00-\x000\x000\x005\x00']打印线条效果很好,但在我尝试拆分线

python:Windows终端中的unicode,使用的编码?

我在Windows7终端中使用Python解释器。我正在努力思考unicode和编码。我输入:>>>s='ë'>>>s'\x89'>>>u=u'ë'>>>uu'\xeb'问题一:为什么字符串s和unicode字符串u使用的编码不同?我继续,然后输入:>>>us=unicode(s)Traceback(mostrecentcalllast):File"",line1,inUnicodeDecodeError:'ascii'codeccan'tdecodebyte0x89inposition0:ordinalnotinrange(128)>>>us=unicode(s,'latin-1')

python - django:gettext 和强制转换为 unicode

我的Django应用程序中有以下代码。classStatus(object):def__init__(self,id,desc):self.id=idself.desc=descdef__unicode__(self):returnself.descSTATUS=Status(0,_(u"Sometext"))当我尝试显示某些状态(或什至将其强制转换为unicode)时,我得到:TypeError:coercingtoUnicode:needstringorbuffer,__proxy__found谁能解释一下,我做错了什么? 最佳答案