Python:二进制文件实现等间隔取相同数据量并合并

举例:每3byte为一页,每3页为一wl。将所有wl的第一页/第二页/第三页分别合并为一个文件。

data = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03\x04\x05\x06\x07\x08\x09'
page_size = 3 #3byte per page
page_per_wl = 3 #3 page per wl
wl_size = len(data) // page_size // page_per_wl #wl cnt

pages = [data[i:i + page_size] for i in range(0, len(data), page_size)] #page data list

first_pages_data = [[] for _ in range(wl_size)]
second_pages_data = [[] for _ in range(wl_size)]
third_pages_data = [[] for _ in range(wl_size)]
for i, page in enumerate(pages):
    team_index = i // page_per_wl #索引
    if i % page_per_wl == 0:  # 处理第一页数据
        first_pages_data[team_index].append(page)
    elif i % page_per_wl == 1:  # 处理第二页数据
        second_pages_data[team_index].append(page)
    elif i % page_per_wl == 2:  # 处理第三页数据
        third_pages_data[team_index].append(page)

if True:
    first_pages_combined = b''.join([page for pages in first_pages_data for page in pages])
    with open('111.bin', 'wb') as lb_file:
        lb_file.write(first_pages_combined)

if True:
    second_pages_combined = b''.join([page for pages in second_pages_data for page in pages])
    with open('222.bin', 'wb') as cb_file:
        cb_file.write(second_pages_combined)

if True:
    third_pages_combined = b''.join([page for pages in third_pages_data for page in pages])
    with open('333.bin', 'wb') as mb_file:
        mb_file.write(third_pages_combined)


你可能感兴趣的:(深度学习与Python,python,数据结构)