Python - Python 2.X Unicode strings file


In Python 2.X, Unicode strings are coded and display with a leading "u," byte.

And strings don't require or show a leading "b,".

Unicode text files must be opened with, which accepts an encoding name just like 3.X's open.

Uses the special unicode string to represent content in memory.

Binary file mode may seem optional in 2.X since normal files are just byte-based data, but it's required to avoid changing line ends if present:


import codecs 
print('unidata.txt', encoding='utf8').read() )     # 2.X: read/decode text 
print( open('unidata.txt', 'rb').read() )                       # 2.X: read raw bytes 
print( open('unidata.txt').read() )                             # 2.X: raw/undecoded too
# from   ww  w  .  jav a  2 s  .  c om


Related Topic