Python-Ref > String manipulation > String encodings > Files and encodings
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

Files and encodings

How does a file look at low level.
On the low level a file does not look the way it looks in a text editor. It is just a continuous sequence of bytes - numbers. As was shown in Basics and the ASCII character set, we need an encoding that says which number represents which character. To create a picture of how a file looks like at the low level, the following example may be used.
Expand/Shrink
This is an example text in an example file.
It even has
several
lines.
Zdroj: (strenc2-1.py)
  1   f = file( "example.txt", "r")
  2   text = f.read()
  3   f.close()
  4   print "---------- This is the text ----------"
  5   print text
  6   
  7   bytes = []
  8   for char in text:
  9       bytes.append( ord( char))
 10   print "---------- These are the bytes ----------"
 11   print bytes
stdout:
---------- This is the text ----------
This is an example text in an example file.
It even has
several
lines.

---------- These are the bytes ----------
[84, 104, 105, 115, 32, 105, 115, 32, 97, 110, 32, 101, 120, 97, 109, 112, 108, 101, 32, 116, 101, 120, 116, 32, 105, 110, 32, 97, 110, 32, 101, 120, 97, 109, 112, 108, 101, 32, 102, 105, 108, 101, 46, 10, 73, 116, 32, 101, 118, 101, 110, 32, 104, 97, 115, 10, 115, 101, 118, 101, 114, 97, 108, 10, 108, 105, 110, 101, 115, 46, 10]
Doba běhu: 19.1 ms