unicode - Python Reference (The Right Way) - Read the Docs

文章推薦指數: 80 %
投票人數:10人

Remarks¶. If encoding and/or errors are given, unicode() will decode the object which can either be an 8-bit string or a character buffer using the codec ... PythonReference(TheRightWay) latest Introduction Definitions CodingGuidelines FundamentalDataTypes Built-InFunctions ComprehensionsandGeneratorExpression ContainerDataAccess Operators Statements OtherObjects DoubleUnderscoreMethodsandVariables Exceptions Constants Boilerplate GlimpseofthePSL Resources Licence PythonReference(TheRightWay) Docs» unicode EditonGitHub unicode¶ Description¶ ReturnstheUnicodestringversionofobject. Syntax¶ unicode(object=’‘) unicode(object[,encoding[,errors]]) object Required.Anobjecttobeconvertedtostring. encoding Optional.Stringencoding. errors Optional.Errors. ReturnValue¶ #TODO TimeComplexity¶ #TODO Remarks¶ Ifencodingand/orerrorsaregiven,unicode()willdecodetheobjectwhichcaneitherbean8-bitstringoracharacterbufferusingthecodecforencoding.Theencodingparameterisastringgivingthenameofanencoding;iftheencodingisnotknown,LookupErrorisraised.Errorhandlingisdoneaccordingtoerrors;thisspecifiesthetreatmentofcharacterswhichareinvalidintheinputencoding.Iferrorsis‘strict’(thedefault),aValueErrorisraisedonerrors,whileavalueof‘ignore’causeserrorstobesilentlyignored,andavalueof‘replace’causestheofficialUnicodereplacementcharacter,U+FFFD,tobeusedtoreplaceinputcharacterswhichcannotbedecoded.Seealsothecodecsmodule. Ifnooptionalparametersaregiven,unicode()willmimicthebehaviourofstr()exceptthatitreturnsUnicodestringsinsteadof8-bitstrings.Moreprecisely,ifobjectisaUnicodestringorsubclassitwillreturnthatUnicodestringwithoutanyadditionaldecodingapplied. Forobjectswhichprovidea__unicode__()method,itwillcallthismethodwithoutargumentstocreateaUnicodestring.Forallotherobjects,the8-bitstringversionorrepresentationisrequestedandthenconvertedtoaUnicodestringusingthecodecforthedefaultencodingin‘strict’mode. Example1¶ >>>unicode(10.25) u'10.25' >>>unicode((1,2)) u'(1,2)' >>>unicode([1,2]) u'[1,2]' >>>unicode({1,2}) u'set([1,2])' >>>unicode({'a':1,'b':2}) u"{'a':1,'b':2}" >>>unicode('foobar') u'foobar' Example2¶ >>>unicode('źdźbło','windows-1250') u'\u0139\u013dd\u0139\u015fb\u0139\u201ao' Example3¶ >>>unicode('źdźbło','ascii','ignore') u'dbo' >>>unicode('źdźbło','ascii','strict') Traceback(mostrecentcalllast): File"",line1,in UnicodeDecodeError:'ascii'codeccan'tdecodebyte0xc5inposition0:ordinalnotinrange(128) >>>unicode('źdźbło','ascii','replace') u'\ufffd\ufffdd\ufffd\ufffdb\ufffd\ufffdo' SeeAlso¶ #TODO ReadtheDocs v:latest Versions latest Downloads pdf htmlzip epub OnReadtheDocs ProjectHome Builds FreedocumenthostingprovidedbyReadtheDocs.



請為這篇文章評分?