繁体   English   中英

我想为python中的文本文件生成unicode UTF-16

[英]I want to generate the unicode UTF-16 for a text file in python

就像在图片中一样,我在一个文件中有一个普通文本,我想用这样的 unicode 编写
在此处输入图片说明

我有这个代码,但它没有完成工作,它只是按原样编写文本,而我确实需要显示 utf-16 编码

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\u0026#39; + '{:04x}'.format(ord(letter)) for letter in line.rstrip('\\n')]).replace('\\\ ',' ') 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM