在Python中将unicode写入二进制文件 [英] writing unicode to binary file in python
问题描述
现在,我想添加一堆字符串到文件的末尾。
编写常规的基于ASCII的字符串很简单:
value =( 'ab')
s = struct.Struct('2s')
packed_data = s.pack(value)
我学习了如何从将字符串解释为打包的二进制数据。
但是,有没有办法做到基于unicode(utf-8)的字符串?
?有没有人做过这个? Unicode!= UTF-8。 UTF-8是Unicode的二进制编码,所以只需要像编写ASCII字符串那样编写UTF-8字符串即可。不需要打包编码的字符串。
$ b
#编码:utf8
导入结构
text = u'我是美国人。'
encoded_text = text.encode('utf8')
#证明包装是多余的...
format ='{0} s' .format(len(encoded_text))
packed_text = struct.pack(format,encoded_text)
print encoded_text == packed_text#result:True
因此,只需要编码你的Unicode字符串,并在写入你的打包整数后将它们追加到文件中。
I'm wondering how to write unicode (utf-8) to a binary file. Here's the background: I've got a 40 byte header (10 ints), and a table with a variable number of triple-int structs. Writing these was cake.
Now, I want to add a bunch of strings to the end of the file.
Writing regular ASCII based strings is easy:
value = ('ab')
s = struct.Struct('2s')
packed_data = s.pack(value)
I learned how to do this from the Interpret strings as packed binary data.
But is there a way to do this for unicode (utf-8) based strings?
Any ideas? Has anyone done this before?
Unicode != UTF-8. UTF-8 is a binary encoding of Unicode, so just write the UTF-8 string just as you would an ASCII string. No need to pack an encoded string either. It's already "just a bunch of bytes".
# coding: utf8
import struct
text = u'我是美国人。'
encoded_text = text.encode('utf8')
# proof packing is redundant...
format = '{0}s'.format(len(encoded_text))
packed_text = struct.pack(format,encoded_text)
print encoded_text == packed_text # result: True
So just encode your Unicode strings and append them to the file after writing your packed ints.
这篇关于在Python中将unicode写入二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!