unicode - Python Reference (The Right Way) - Read the Docs
文章推薦指數: 80 %
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"
延伸文章資訊
- 1unicode - Python Reference (The Right Way) - Read the Docs
Remarks¶. If encoding and/or errors are given, unicode() will decode the object which can either ...
- 2Unicode & Character Encodings in Python: A Painless Guide
Encoding and Decoding in Python 3 ... Python 3's str type is meant to represent human-readable te...
- 3A Guide to Unicode, UTF-8 and Strings in Python | by Sanket Gupta
- 4unicode - Python Reference (The Right Way) - Read the Docs
- 5Unicode HOWTO — Python 3.10.7 documentation
Since Python 3.0, the language's str type contains Unicode characters, meaning any string created...