Python:1 和 0 组成的字符串 ->二进制文件

发布于 2024-12-09 08:01:58 字数 104 浏览 0 评论 0原文

我在 Python 中有一个由 1 和 0 组成的字符串,我想将其写入二进制文件。我在寻找一个好方法来做到这一点时遇到了很多麻烦。

有没有一种我只是缺少的标准方法可以做到这一点?

I have a string of 1's and 0's in Python and I would like to write it to a binary file. I'm having a lot of trouble with finding a good way to do this.

Is there a standard way to do this that I'm simply missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愛上了 2024-12-16 08:01:58

如果您想要一个二进制文件,

>>> import struct
>>> myFile=open('binaryFoo','wb')
>>> myStr='10010101110010101'
>>> x=int(myStr,2)
>>> x
76693
>>> struct.pack('i',x)
'\x95+\x01\x00'
>>> myFile.write(struct.pack('i',x))
>>> myFile.close()
>>> quit()
$ cat binaryFoo
�+$

这是您正在寻找的吗?

If you want a binary file,

>>> import struct
>>> myFile=open('binaryFoo','wb')
>>> myStr='10010101110010101'
>>> x=int(myStr,2)
>>> x
76693
>>> struct.pack('i',x)
'\x95+\x01\x00'
>>> myFile.write(struct.pack('i',x))
>>> myFile.close()
>>> quit()
$ cat binaryFoo
�+$

Is this what you are looking for?

街道布景 2024-12-16 08:01:58
In [1]: int('10011001',2)
Out[1]: 153

将输入拆分为八位,然后应用 int(_, 2)chr,然后连接成一个字符串并将该字符串写入文件。

像...的东西:

your_file.write(''.join(chr(int(your_input[8*k:8*k+8], 2)) for k in xrange(len(your_input)/8)))
In [1]: int('10011001',2)
Out[1]: 153

Split your input into pieces of eight bits, then apply int(_, 2) and chr, then concatenate into a string and write this string to a file.

Something like...:

your_file.write(''.join(chr(int(your_input[8*k:8*k+8], 2)) for k in xrange(len(your_input)/8)))
梦里兽 2024-12-16 08:01:58

现在有一个 bitstring 模块可以满足您的需要。

from bitstring import BitArray

my_str = '001001111'
binary_file = open('file.bin', 'wb')
b = BitArray(bin=my_str)
b.tofile(binary_file)
binary_file.close()

您可以使用 xxd -b file.bin 在 Linux 的 shell 中测试它

There is a bitstring module now which does what you need.

from bitstring import BitArray

my_str = '001001111'
binary_file = open('file.bin', 'wb')
b = BitArray(bin=my_str)
b.tofile(binary_file)
binary_file.close()

You can test it from the shell in Linux with xxd -b file.bin

小耗子 2024-12-16 08:01:58

或者您可以像这样使用 array 模块

$ python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random,array
#This is the best way, I could think of for coming up with an binary string of 100000 
>>> binStr=''.join([str(random.randrange(0,2)) for i in range(100000)]) 
>>> len(binStr)
100000
>>> a = array.array("c", binStr)
#c is the type of data (character)
>>> with open("binaryFoo", "ab") as f:
...     a.tofile(f)
... 
#raw writing to file
>>> quit()
$ 

Or you can use the array module like this

$ python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random,array
#This is the best way, I could think of for coming up with an binary string of 100000 
>>> binStr=''.join([str(random.randrange(0,2)) for i in range(100000)]) 
>>> len(binStr)
100000
>>> a = array.array("c", binStr)
#c is the type of data (character)
>>> with open("binaryFoo", "ab") as f:
...     a.tofile(f)
... 
#raw writing to file
>>> quit()
$ 
冰葑 2024-12-16 08:01:58
BITS_IN_BYTE = 8
chars = '00111110'
bytes = bytearray(int(chars[i:i+BITS_IN_BYTE], 2)
    for i in xrange(0, len(chars), BITS_IN_BYTE))
open('filename', 'wb').write(bytes)
BITS_IN_BYTE = 8
chars = '00111110'
bytes = bytearray(int(chars[i:i+BITS_IN_BYTE], 2)
    for i in xrange(0, len(chars), BITS_IN_BYTE))
open('filename', 'wb').write(bytes)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文