str([object]) 返回一个包含 对象的可打印表示的字符串。对于字符串,这将返回字符串本身。与repr(对象)的差异 是str(对象)并不总是尝试 返回eval()可接受的字符串;它的目标是返回一个 可打印字符串。如果没有给出参数,则返回空字符串, ''''。 现在我们在windows下尝试这个: Traceback(最近一次调用最后一次): 文件"< stdin>",第1行,< module> UnicodeEncodeError:'' ascii''编解码器不能编码字符u''\863'' 位置0 :序数不在范围内(128) 在3.0这是固定的: ''\\\ '866'' " b''123''" 这样的问题至少部分促使改为unicode 而不是字符串类型的字节。 tjr From python manualstr( [object])Return a string containing a nicely printable representation of anobject. For strings, this returns the string itself. The differencewith repr(object) is that str(object) does not always attempt toreturn a string that is acceptable to eval(); its goal is to return aprintable string. If no argument is given, returns the empty string,''''.now we try this under windows:Traceback (most recent call last):File "<stdin>", line 1, in <module>UnicodeEncodeError: ''ascii'' codec can''t encode character u''\ue863'' inposition 0: ordinal not in range(128)FAIL.also almighty LinuxPython 2.3.4 (#1, Feb 6 2006, 10:38:46)[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):File "<stdin>", line 1, in ?UnicodeEncodeError: ''ascii'' codec can''t encode character u''\ue863'' inposition 0: ordinal not in range(128)Python 2.4.4 (#2, Apr 5 2007, 20:11:18)[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):File "<stdin>", line 1, in ?UnicodeEncodeError: ''ascii'' codec can''t encode character u''\ue863'' inposition 0: ordinal not in range(128)Python 2.5 (release25-maint, Jul 20 2008, 20:47:25)[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):File "<stdin>", line 1, in <module>UnicodeEncodeError: ''ascii'' codec can''t encode character u''\ue863'' inposition 0: ordinal not in range(128)The problem is, why the f**k set ASCII encoding to range(128) ????????while str() is internally byte array it should be handled inrange(256) !!!!!!!!!! http://bugs.python.org/issue3648One possible solution(Windows Only)''\xfe\x9f''t?I now spending 60% of my developing time dealing with ASCII range(128)errors. It was PAIN!!!!!!Please fix this issue. http://bugs.python.org/issue3648Please. 解决方案Because that''s how ASCII is defined.But that''s for random bytes. How would you convert an arbitrary object torandom bytes?Because that''s how ASCII is defined. ASCII is a 7-bit code.Yes `str` can handle that, but that''s not the point. The point is how totranslate the contents of a `unicode` object into that range. There aremany different possibilities and Python refuses to guess and tries thelowest common denominator -- ASCII -- instead.The issue was closed as ''invalid''. Dealing with Unicode can be a painand frustrating, but that''s not a Python problem, it''s the subject itselfthat needs some thoughts. If you think this through, the relationshipbetween characters, encodings, and bytes, and stop dreaming of a magicsolution that works without dealing with this stuff explicitly, the painwill go away -- or ease at least.Ciao,Marc ''BlackJack'' Rintschstr( [object])Return a string containing a nicely printable representation of anobject. For strings, this returns the string itself. The differencewith repr(object) is that str(object) does not always attempt toreturn a string that is acceptable to eval(); its goal is to return aprintable string. If no argument is given, returns the empty string,''''.now we try this under windows:Traceback (most recent call last): File "<stdin>", line 1, in <module>UnicodeEncodeError: ''ascii'' codec can''t encode character u''\ue863'' inposition 0: ordinal not in range(128)In 3.0 this is fixed:''\ue863''"b''123''"Problems like this at least partly motivated the change to unicodeinstead of bytes as the string type.tjr 这篇关于str()应该将ANY对象转换为没有EXCEPTIONS的字符串!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:02
查看更多