Python-将宽字符字符串从二进制文件转换为Python unicode字符串 [英] Python - converting wide-char strings from a binary file to Python unicode strings

查看:248
本文介绍了Python-将宽字符字符串从二进制文件转换为Python unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天已经很漫长了,我有点沮丧.

It's been a long day and I'm a bit stumped.

我正在读取一个包含很多宽字符字符串的二进制文件,我想将它们作为Python unicode字符串转储出去. (要解压缩非字符串数据,我使用的是struct模块,但我不对字符串进行相同的操作.)

I'm reading a binary file that contains lots of wide-char strings and I want to dump these out as Python unicode strings. (To unpack the non-string data I'm using the struct module, but I don't how to do the same with the strings.)

例如,阅读系列"一词:

For example, reading the word "Series":

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

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

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

How can I encode that raw wide-char data as a Python string?

我正在使用Python 2.6

I'm using Python 2.6

推荐答案

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

这篇关于Python-将宽字符字符串从二进制文件转换为Python unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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