【问题标题】:Hello, i have a text file that i want to write in UTF-16 unicode using python您好,我有一个文本文件,我想使用 python 以 UTF-16 unicode 编写
【发布时间】:2020-05-31 01:30:41
【问题描述】:

例如我从文件 1 中读取:

ch="Hello world, this a stackoverflow example"

我在文件 2 中写入 unicode UTF-16,输出必须是这样的:

output="\u0048\u0065\u006c\u006c\u006f \u0077\u006f\u0072\u006c\u0064\u002c \u0074\u0068\u0069\u0073 \u0061 \u0073\u0074\u0061\u0063\u006b\u006f\u0076\u0065\u0072 \u0066\u006c\u006f\u0077 \u0065\u0078\u0061\u006d\u0070\u006c\u0065"

我找到了如何转换或阅读,但没有找到如何转换

【问题讨论】:

  • 请说明您是否希望将 ch 字符串以字节编码为 UTF-16,或 文字字符 反斜杠、u0048等文件中。

标签: python unicode utf-16


【解决方案1】:

当你open你的输出文件时,只需传递encoding="utf-16"

ch="Hello world, this a stackoverflow example"

with open("utf_16.txt", "w", encoding="utf-16") as f:
   f.write(ch)
$ file utf_16.txt 
utf_16.txt: Little-endian UTF-16 Unicode text, with no line terminators

$ hexdump -Cv utf_16.txt 
00000000  ff fe 48 00 65 00 6c 00  6c 00 6f 00 20 00 77 00  |..H.e.l.l.o. .w.|
00000010  6f 00 72 00 6c 00 64 00  2c 00 20 00 74 00 68 00  |o.r.l.d.,. 
.t.h.|
...

请注意,utf-16 编码包含字节顺序标记 (BOM)。如果您不想这样做,请在编码名称中包含字节顺序(例如utf-16le):

ch="Hello world, this a stackoverflow example"

with open("utf_16.txt", "w", encoding="utf-16le") as f:
   f.write(ch)
$ file utf_16.txt 
utf_16.txt: data

$ hexdump -Cv utf_16.txt 
00000000  48 00 65 00 6c 00 6c 00  6f 00 20 00 77 00 6f 00  |H.e.l.l.o. .w.o.|
00000010  72 00 6c 00 64 00 2c 00  20 00 74 00 68 00 69 00  |r.l.d.,. .t.h.i.|
...

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2013-08-27
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多