在python中将ASCII字符串作为二进制写入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29151181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:08:37  来源:igfitidea点击:

Writing an ASCII string as binary in python

pythonfilebinaryfwrite

提问by aMa

I have a ASCII string = "abcdefghijk". I want to write this to a binary file in binary format using python.

我有一个 ASCII 字符串 =“abcdefghijk”。我想使用python将其写入二进制格式的二进制文件。

I tried following:

我尝试了以下操作:

str  = "abcdefghijk"
fp = file("test.bin", "wb")
hexStr = "".join( (("\x%s") % (x.encode("hex"))) for x in str)
fp.write(hexStr)
fp.close()

However, when I open the test.bin I see the following in ascii format instead of binary.

但是,当我打开 test.bin 时,我看到以下内容是 ascii 格式而不是二进制格式。

\x61\x62\x63\x64\x65\x66\x67

I understand it because for two slashes here ("\\x%s"). How could I resolve this issue? Thanks in advance.

我理解它,因为这里有两个斜杠(“\\x%s”)。我该如何解决这个问题?提前致谢。

Update :

更新 :

Following gives me the expected result:

以下给了我预期的结果:

file = open("test.bin", "wb")
file.write("\x61\x62\x63\x64\x65\x66\x67")
file.close() 

But how do I achieve this with "abcdef" ASCII string. ?

但是我如何使用“abcdef”ASCII 字符串来实现这一点。?

采纳答案by Martijn Pieters

You misunderstood what \xhhdoes in Python strings. Using \xnotation in Python strings is just syntaxto produce certain codepoints.

您误解了\xhhPython 字符串的作用。\x在 Python 字符串中使用符号只是生成某些代码点的语法

You can use '\x61'to produce a string, or you can use 'a'; both are just two ways of saying give me a string with a character with hexadecimal value 61, e.g. the aASCII character:

您可以使用'\x61'来生成字符串,也可以使用'a'; 两者都只是说给我一个带有十六进制值 61 字符的字符串的a两种方式,例如ASCII 字符

>>> '\x61'
'a'
>>> 'a'
'a'
>>> 'a' == '\x61'
True

The \xhhsyntax then, is not the value; there is no \and no xand no 6and 1character in the final result.

\xhh随后的语法,是不是值; 最终结果中没有\和没有x,没有61字符。

You should just write your string:

你应该只写你的字符串

somestring = 'abcd'

with open("test.bin", "wb") as file:
    file.write(somestring)

There is nothing magical about binary files; the only difference with a file opened in text mode is that a binary file will not automatically translate \nnewlines to the line separator standard for your platform; e.g. on Windows writing \nproduces \r\ninstead.

二进制文件没有什么神奇之处。与以文本模式打开的文件的唯一区别是二进制文件不会自动将\n换行符转换为适用于您平台的行分隔符标准;例如,在 Windows 上写作\n会产生\r\n

You certainly do not have to produce hexadecimal escapes to write binary data.

您当然不必生成十六进制转义符来写入二进制数据。

On Python 3 strings are Unicode data and cannot just be written to a file without encoding, but on Python the strtype is alreadyencoded bytes. So on Python 3 you'd use:

在 Python 3 上,字符串是 Unicode 数据,不能不经过编码就写入文件,但在 Python 上,该str类型已经是编码字节。所以在 Python 3 上你会使用:

somestring = 'abcd'

with open("test.bin", "wb") as file:
    file.write(somestring.encode('ascii'))

or you'd use a byte string literal; b'abcd'.

或者你会使用字节字符串文字;b'abcd'.

回答by Joran Beasley

I think you don't necessarily understand what binary/ascii is ... all files are binary in the sense that its just bits. ascii is just a representation of some bits... 99.9999 % of file editors will display your bits as ascii if they can , and if there is no other encoding declared in the file itself ...

我认为您不一定了解二进制/ascii 是什么......所有文件都是二进制的,因为它只是位。ascii 只是一些位的表示... 99.9999 % 的文件编辑器将您的位显示为 ascii,如果可以,并且文件本身没有声明其他编码...

fp.write("abcd") 

is exactly equivelentto

正好equivelent

fp.write("\x61\x62\x63\x64")