Python - 将宽字符字符串从二进制文件转换为 Python unicode 字符串

标签 python string binaryfiles

这是漫长的一天,我有点难过。

我正在读取一个包含大量宽字符字符串的二进制文件,我想将它们转储为 Python unicode 字符串。 (为了解压非字符串数据,我使用了 struct 模块,但我不知道如何对字符串执行相同的操作。)

例如读“系列”这个词:

myfile = open("test.lei", "rb")
myfile.seek(44)
data = myfile.read(12)

# data is now 'S\x00e\x00r\x00i\x00e\x00s\x00'

如何将原始宽字符数据编码为 Python 字符串?

编辑:我使用的是 Python 2.6

最佳答案

>>> data = 'S\x00e\x00r\x00i\x00e\x00s\x00'
>>> data.decode('utf-16')
u'Series'

关于Python - 将宽字符字符串从二进制文件转换为 Python unicode 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2746426/

相关文章:

python - Tensorflow csv读取器错误: "Quote inside a string has to be escaped by another quote"

regex - 如何在Golang中验证以逗号分隔的字符串?

java - 我如何调用 Intent 并将其放入我的 vector 中?

file-io - 从Julia的二进制文件中读取4字节字

python - 使用 Python/Cython 编写二进制文件的更快方法

c++ - 在 C++ 中将大数据读写到自定义二进制文件

python - 我得到一个 AttributeError : module 'collections' has no attribute 'Iterable' when I try to install libraries using PIP

python - 发票行总和 - Odoo v8

javascript - 允许用户继续使用终端的终端应用程序

编译中间有空终止符的 char 数组