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 in Python 2
Unicode strings are sequences of platonic characters ... import codecs codecs.encode() codecs.dec...
- 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...
- 3Unicode HOWTO — Python 3.10.7 documentation
Since Python 3.0, the language's str type contains Unicode characters, meaning any string created...
- 4Unicode 指南— Python 3.10.7 說明文件
还有有关显示的属性,比如如何在双向文本中使用码位。 以下程序显示了几个字符的信息,并打印一个字符的数值:. import unicodedata ...
- 5Unicode — pysheeet
In Python 3, strings are represented by Unicode instead of bytes. ... get u2 byte string b'Cafe\x...