我想为python中的文本文件生成Unicode UTF-16 [英] I want to generate the unicode UTF-16 for a text file in python

查看:78
本文介绍了我想为python中的文本文件生成Unicode UTF-16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像在图片中一样,我在文件中有一个普通文本,我想用unicode来写

like in the picture, i have a normal text in a file that i want to write in unicode like that

我正在使用此代码,但是它不能完成工作,它只是按原样编写文本,而我确实需要显示utf-16编码

i'm having this code, but it doesn't do the job, it just write the text as it is while i need really the utf-16 encoding to be displayed

with open(localOutputPath,'r') as infile:
        data = infile.read()
        #print(data)

    with open(localUtf16Path, 'w', encoding="utf-16") as outfile:
        outfile.write(data)

推荐答案

完全没用:

==> 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

这篇关于我想为python中的文本文件生成Unicode UTF-16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆