4 down vote accepted

You misunderstood what \xhh does in Python strings. Using \x notation in Python strings is just syntax to produce certain codepoints.

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 a ASCII character:

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

The \xhh syntax then, is not the value; there is no \ and no x and no 6 and 1 character in the final result.

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 \n newlines to the line separator standard for your platform; e.g. on Windows writing \n produces \r\n instead.

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 str type is already encoded bytes. So on Python 3 you'd use:

somestring = 'abcd'

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

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

python string写入二进制文件——直接wb形式open file,再write string即可的更多相关文章

  1. Python 读取写入配置文件 —— ConfigParser

    Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...

  2. C# 写入二进制文件

    写入整型25 文件在MiniHex中显示 写入字符串I am happy 0A 6D - 6D - 这一行数据是C#把字符串转换为16进制形式 不知道为啥用MiniHex打开多了个0A 写入空&quo ...

  3. TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法

    Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...

  4. python爬虫如何POST request payload形式的请求

    python爬虫如何POST request payload形式的请求1. 背景最近在爬取某个站点时,发现在POST数据时,使用的数据格式是request payload,有别于之前常见的 POST数 ...

  5. 读取 java.nio.ByteBuffer 中的字符串(String) 写入方式flash.utils.ByteArray.writeUTF

    通过研究ByteArray的写入格式以及方法说明,可以发现writeUTF是先使用2位写入字符串的长度,然后在其后写入字符串编码. flash.utils.ByteArray.writeUTF(val ...

  6. python换行写入文件

    今天用python做写入文件时,碰到,写入的东西不能换行,打开写入的文件都是一行.后来发现需要在写入的字符后面加上+'\n'. 另外python需要追加写入文件的时候,是用这个方法f = open(' ...

  7. 当提交的表单类型为multipart/form-data时 后台的dopost则不能使用 setCharset来进行解码了 需要单独对字段使用 原始的new String(req.name("ISO-8859-1"),"utf-8")形式解码了

    当提交的表单类型为multipart/form-data时 后台的dopost则不能使用 setCharset来进行解码了 需要单独对字段使用 原始的new String(req.name(" ...

  8. 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法

    一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...

  9. python数据写入Excel表格

    from openpyxl import Workbook def main(): sheet_name = "表名1" row_count = 6 # 行数 info_resul ...

随机推荐

  1. Ubuntu 16.04下使用Wine安装正则表达式工具RegexBuddy 4

    说明: 1.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 2.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 下载: (链接 ...

  2. erlang debugger

    http://erlang.org/doc/apps/debugger/debugger_chapter.html

  3. INDY9发送tstream

    INDY9发送tstream 首先都要发送stream.Size, 这是必须的. // 服务端 AThread.Connection.WriteInteger(stream2.Size); AThre ...

  4. ios构造和析构

    遵循规则: 构造先父类后子类 析构先子类后父类 所以,自定义的init函数需要首先[super init....] dealloc中[super dealloc]却是放在最后的 - (id)initW ...

  5. Android Studio 删除项目

    在项目上右键 点击“Open Module Settings”,然后你会看到你的项目排成一列,如果想删除哪个,点击项目,然后在左上角,点击“-”号,然后返回后发现这个项目变为灰色,点击项目右键,看到“ ...

  6. gitignore 规则

    在使用git的过程中,总有一些我们不想被跟踪的文件,例如vim的交换文件,编译产生的文件等等.这时,我们可以在项目的根目录下创建一个名为 .gitignore 的文件,列出不想被跟踪的文件模式即可.下 ...

  7. sublime的tab和spaces空格切换的坑

    python是严格要求对齐或者叫缩进的: 使用sublime对python进行编程时,可以使用tab或者空格,但是不能混用.特别是从外面把代码拷贝进sublime的时候,更要注意是否一致. 简单介绍一 ...

  8. SVN服务器配置说明 【转】

    http://www.cnblogs.com/ricksun/articles/1564905.html 1.前 言 花了72小时,终于把 Subversion 初步掌握了.从一个连“什么是版本控制” ...

  9. Opencv 最小外接矩形合并拼接

    前一篇画出了最小外接矩形,但是有时候画出来的矩形由于中间像素干扰或者是其他原因矩形框并不是真正想要的 如图1是一个信号的雨图,被矩形框分割成了多个小框: 需要合并矩形框达到的效果: 主要思想: 扫描两 ...

  10. 解题报告 之 HDU5288 OO&#39; s Sequence

    解题报告 之 HDU5288 OO' s Sequence Description OO has got a array A of size n ,defined a function f(l,r) ...