如何在 Python 的字符串中查找空字节? [英] How to find null byte in a string in Python?

查看:107
本文介绍了如何在 Python 的字符串中查找空字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读取文件后解析数据时遇到问题.我正在做的是读取一个二进制文件,并需要从读取的文件中创建一个属性列表,文件中的所有数据都以空字节终止.我想要做的是找到空字节终止属性的每个实例.

I'm having an issue parsing data after reading a file. What I'm doing is reading a binary file in and need to create a list of attributes from the read file all of the data in the file is terminated with a null byte. What I'm trying to do is find every instance of a null byte terminated attribute.

本质上是一个字符串

Health\x00experience\x00charactername\x00

并将其存储在列表中.

真正的问题是我需要保持空字节完整,我只需要能够找到空字节的每个实例并存储它之前的数据.

The real issue is I need to keep the null bytes in tact, I just need to be able to find each instance of a null byte and store the data that precedes it.

推荐答案

虽然归结为使用 split('\x00') 一个方便的包装器可能会很好.

While it boils down to using split('\x00') a convenience wrapper might be nice.

def readlines(f, bufsize):
    buf = ""
    data = True
    while data:
        data = f.read(bufsize)
        buf += data
        lines = buf.split('\x00')
        buf = lines.pop()
        for line in lines:
            yield line + '\x00'
    yield buf + '\x00'

然后你可以做类似的事情

then you can do something like

with open('myfile', 'rb') as f:
    mylist = [item for item in readlines(f, 524288)]

这有一个额外的好处,即在拆分文本之前不需要将整个内容加载到内存中.

This has the added benefit of not needing to load the entire contents into memory before splitting the text.

这篇关于如何在 Python 的字符串中查找空字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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