file close( characters written text open('unidata txt'encoding='utf- 'read(text 'spamlen(text read/decode utf- text chars (code pointsthis automatic encoding and decoding is what you normally want because files handle this on transfersyou may process text in memory as simple string of characters without concern for its unicode-encoded origins if neededthoughyou can also see what' truly stored in your file by stepping into binary moderead raw encoded bytes raw open('unidata txt''rb'read(raw 'sp\xc \ mlen(raw really bytes in utf- you can also encode and decode manually if you get unicode data from source other than file--parsed from an email message or fetched over network connectionfor exampletext encode('utf- ' 'sp\xc \ mraw decode('utf- ''spammanual encode to bytes manual decode to str this is also useful to see how text files would automatically encode the same string differently under different encoding namesand provides way to translate data to different encodings--it' different bytes in filesbut decodes to the same string in memory if you provide the proper encoding nametext encode('latin- ' 'sp\xc mtext encode('utf- ' '\xff\xfes\ \ \xc \ \ bytes differ in others len(text encode('latin- '))len(text encode('utf- ')( '\xff\xfes\ \ \xc \ \ decode('utf- ''spambut same string decoded this all works more or less the same in python xbut unicode strings are coded and display with leading " ,byte strings don' require or show leading " ,and unicode text files must be opened with codecs openwhich accepts an encoding name just like ' openand uses the special unicode string to represent content in memory binary file mode may seem optional in since normal files are just byte-based databut it' required to avoid changing line ends if present (more on this later in the book)files