I am trying to write a program that can run through both python 2 & 3. It reads character from website and writes into file. I have already imported unicode_literals
from __future__.
Straight out trying to write a string that looks like this:
txt = u'his$\u2026\n'
Will result in UnicodeEncodeError:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 4: ordinal not in range(128)
The only way to write it to a file in python2 is:
fp = open("/tmp/test", "w")
txt2 = txt.encode('utf-8')
fp.write(txt2) # It works
type(txt2) # str - that is why it works
However, trying to reuse the same code in python3 is not going to work since in python 3,
type(txt2) # is byte type
E.g
txt.encode('utf-8')
b'his$\xe2\x80\xa6\n'
Forcing a fp.write(txt2)
will throw TypeError:
TypeError: write() argument must be str, not bytes
So, cantxt = u'his$\u2026\n'
be written in a file using the same code block in both python 2 and 3. (Other than using a wrapper on fp.write)