用python连接字节列表 [英] Joining byte list with python

查看:180
本文介绍了用python连接字节列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一种工具,该工具可以读取二进制文件,进行一些更改并将其保存.我要做的是为文件中的每一行创建一个列表,处理几行,然后再次加入该列表.

I'm trying to develop a tool that read a binary file, makes some changes and save it. What I'm trying to do is make a list of each line in the file, work with several lines and then join the list again.

这是我尝试过的:

file = open('myFile.exe', 'r+b')

aList = []
for line in f:
    aList.append(line)

#Here im going to mutate some lines.

new_file = ''.join(aList)

并给我这个错误:

TypeError: sequence item 0: expected str instance, bytes found

这很有意义,因为我正在处理字节.

which makes sense because I'm working with bytes.

有没有一种方法可以使用连接功能o与连接字节类似的东西? 谢谢.

Is there a way I can use join function o something similar to join bytes? Thank you.

推荐答案

使用b''.join()对字节字符串执行连接:

Perform the join on a byte string using b''.join():

>>> b''.join([b'line 1\n', b'line 2\n'])
b'line 1\nline 2\n'

这篇关于用python连接字节列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆