Problems with struct.pack()
rcdailey at gmail.com
Thu Oct 11 05:19:49 CEST 2007
Thanks for responding. I apologize about my lack of details, I was in a
hurry when I wrote the initial question. I'll provide more details.
Basically, I'm attempting to write out unicode strings (16 bits per
character) to a file. Before each string, I write out 4 bytes containing the
number of characters (NOT BYTES) the string contains. I suppose the
confusion comes in because I'm writing out both text information AND binary
data at the same time. I suppose the consistent thing to do would be to
write out the strings as binary instead of as text? I'm originally a C++
programmer and I'm still learning Python, so figuring out this problem is a
little difficult for me.
In my initial inquiry, I was writing out 5000 as an example, however this
number would realistically be the number of characters in the string: len(
u"Hello World" ). Once I write out these 4 bytes, I then write out the
string "Hello World" immediately after the 4 bytes. You may be wondering why
the crazy file format. The reason is because this python script is writing
out data that will later be read in by a C++ application.
The following works fine for ASCII strings:
mystring = "Hello World"
file = open( "somefile.txt", "wb" )
file.write( struct.pack ( "I", len(mystring) ) )
file.write( mystring )
Again I do apologize for the lack of detail. If I've still been unclear
please don't hesitate to ask for more details.
On 10/10/07, Carsten Haese <carsten at uniqsys.com> wrote:
> On Wed, 2007-10-10 at 19:00 -0500, Robert Dailey wrote:
> > Hi,
> > I have opened a unicode file for writing:
> > import codecs
> > file = codecs.open( "somefile.dat", "wb", "utf-16" )
> > and I attempt to do this:
> > file.write( struct.pack ( "I", 5000 ) )
> > However, this won't work because the encoding of the string returned
> > by "pack" isn't unicode. I'm a bit confused right now as to how I can
> > get this to work. I can't allow the unicode-ness of the file interfere
> > with the actual order of the bytes that pack() is returning (In other
> > words, don't pack NULL characters in there)
> Please start at the beginning. What are you actually trying to
> accomplish and why do you think you need to write arbitrary binary data
> into a file that is supposed to be a UTF-16 encoded Unicode text file?
> Carsten Haese
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list