Skip to content Skip to sidebar Skip to footer

I Want To Generate The Unicode Utf-16 For A Text File In Python

like in the picture, i have a normal text in a file that i want to write in unicode like that i'm having this code, but it doesn't do the job, it just write the text as it is whil

Solution 1:

Utterly useless:

==> type .\SO\62111029.py
import io
localOutputPath = r'd:\bat\62111029input.txt'
localUtf16Path  = r'd:\bat\62111029output.txt'
data = []
with io.open(localOutputPath, mode="r", encoding="utf-8") as infile:
    with io.open(localUtf16Path, mode="w", encoding="utf-8") as outfile:
        for line in infile:
            data = ''.join(['\\u' + '{:04x}'.format(ord(letter))
                for letter in line.rstrip('\n')]).replace('\\u0020',' ')
            outfile.write(data + '\n')
==> 2>NUL del 62111029output.txt

==> type 62111029input.txt
September 1, 1939
1 Σεπτεμβρίου 1939
1 сентября 1939
1. září 1939
==> .\SO\62111029.py

==> type 62111029output.txt

\u0053\u0065\u0070\u0074\u0065\u006d\u0062\u0065\u0072 \u0031\u002c \u0031\u0039\u0033\u0039
\u0031 \u03a3\u03b5\u03c0\u03c4\u03b5\u03bc\u03b2\u03c1\u03af\u03bf\u03c5 \u0031\u0039\u0033\u0039
\u0031 \u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044f \u0031\u0039\u0033\u0039
\u0031\u002e \u007a\u00e1\u0159\u00ed \u0031\u0039\u0033\u0039

Post a Comment for "I Want To Generate The Unicode Utf-16 For A Text File In Python"