Prev: how to generate a csr in python?
Next: DOWNLOAD FREE PALTALK LIVE VIDEO AND VOICE CHAT SOFTWARE
From: eskandari on 31 May 2010 11:56 Hi, I am a newbie in python. I have an data.pickle file which is serialized form of an "array of strings", I want to write their offsets in another binary file, so an C++ program can read and analyse them. But when I try to write offset (number) in binary file, it raise exception below in line "offsetfile.write(offset)" "TypeError: argument 1 must be string or read-only buffer, not int" I search the internet, find that all suggest converting number to string ---with str()---and then write string to file. But I shouldn't do this. because the above mentioned C++ function, read file with this assumption that there are numbers in file. So I want to know, Is there any way to produce an binary file containing numbers same as the way C++ does? Can anybody help me? Thanks a lot.
From: MRAB on 31 May 2010 12:30 eskandari wrote: > Hi, > I am a newbie in python. I have an data.pickle file which is > serialized form of an "array of strings", I want to write their > offsets in another binary file, so an C++ program can read and analyse > them. > But when I try to write offset (number) in binary file, it raise > exception below in line "offsetfile.write(offset)" > "TypeError: argument 1 must be string or read-only buffer, not int" > > I search the internet, find that all suggest converting number to > string ---with str()---and then write string to file. > But I shouldn't do this. because the above mentioned C++ function, > read file with this assumption that there are numbers in file. > So I want to know, Is there any way to produce an binary file > containing numbers same as the way C++ does? > Can anybody help me? > You can't write ints to a file, but you can write bytestrings ('str' in Python 2, 'bytes' in Python 3). Use the 'struct' module to convert the int to a bytestring, and remember to open the file as a binary file.
From: Tim Chase on 31 May 2010 12:42 On 05/31/2010 10:56 AM, eskandari wrote: > But when I try to write offset (number) in binary file, it raise > exception below in line "offsetfile.write(offset)" > "TypeError: argument 1 must be string or read-only buffer, not int" > > I search the internet, find that all suggest converting number to > string ---with str()---and then write string to file. > But I shouldn't do this. because the above mentioned C++ function, > read file with this assumption that there are numbers in file. > So I want to know, Is there any way to produce an binary file > containing numbers same as the way C++ does? Well, you have at least two options: 1) use the pack/unpack functions in the "struct" module to convert a number to a byte representation that you can then write to a file 2) write the number to the file as a string and then use C++ libraries to parse a number from a string. In both cases, you have to consider what happens when the number is outside the bounds of your C++ data-type (you don't mention what you're using...an int, a long, or "long long"; signed vs. unsigned). Additionally in the first case, you have to make sure that your memory-architecture (big-endian vs. little-endian) matches on both sides; or that you marshal the data through a pre-defined format (in libraries, commonly called "network" format). For such reasons, I'd stick with method #2 unless you have a strong reason not to. -tkc
From: eskandari on 31 May 2010 12:43 On May 31, 12:30 pm, MRAB <pyt...(a)mrabarnett.plus.com> wrote: > eskandari wrote: > > Hi, > > I am a newbie in python. I have an data.pickle file which is > > serialized form of an "array of strings", I want to write their > > offsets in another binary file, so an C++ program can read and analyse > > them. > > But when I try to write offset (number) in binary file, it raise > > exception below in line "offsetfile.write(offset)" > > "TypeError: argument 1 must be string or read-only buffer, not int" > > > I search the internet, find that all suggest converting number to > > string ---with str()---and then write string to file. > > But I shouldn't do this. because the above mentioned C++ function, > > read file with this assumption that there are numbers in file. > > So I want to know, Is there any way to produce an binary file > > containing numbers same as the way C++ does? > > Can anybody help me? > > You can't write ints to a file, but you can write bytestrings ('str' in > Python 2, 'bytes' in Python 3). > > Use the 'struct' module to convert the int to a bytestring, and remember > to open the file as a binary file. Thanks alot, I have an question, if I do so, Will the second program (C++ program) which process this file, encounter any problem while parsing the file? It find number of integers by filelen/4 and ..... (It assumes that file was created as the same way which C++ does) Thanks in advance
From: Terry Reedy on 31 May 2010 13:23 On 5/31/2010 12:43 PM, eskandari wrote: > On May 31, 12:30 pm, MRAB<pyt...(a)mrabarnett.plus.com> wrote: >> eskandari wrote: >> Use the 'struct' module to convert the int to a bytestring, and remember >> to open the file as a binary file. > > Thanks alot, > I have an question, if I do so, Will the second program (C++ program) > which process this file, encounter any problem while parsing the file? > It find number of integers by filelen/4 and ..... (It assumes that > file was created as the same way which C++ does) Tim Chase pretty well answered this. If you are not familiar with the problem of 'endianess', see http://en.wikipedia.org/wiki/Endianess
|
Next
|
Last
Pages: 1 2 Prev: how to generate a csr in python? Next: DOWNLOAD FREE PALTALK LIVE VIDEO AND VOICE CHAT SOFTWARE |