You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何用正则或条件语句实现二进制数据指定转换?

二进制序列批量转换实现方案

针对你需要的二进制转换规则(01→01、011→001、0111→0001、01111→00001),这里提供两种一次性完成的方法,适合刚学习Python的你理解:

方法一:正则表达式(推荐,代码简洁)

利用正则的贪婪匹配特性,优先匹配最长的符合规则的子串,再通过自定义替换逻辑完成转换:

import re

# 读取二进制文件内容(替换为你的文件名)
with open("binary_data.txt", "r") as f:
    binary_content = f.read().strip()

# 定义替换逻辑:根据匹配到的子串长度生成对应结果
def convert_match(match_obj):
    matched_str = match_obj.group()
    # 子串长度:2对应01,3对应011,依此类推
    replace_len = len(matched_str)
    # 生成替换后的字符串:0 + (长度-2)个0 + 1
    return "0" + "0" * (replace_len - 2) + "1"

# 正则匹配规则:0后面跟1-4个1,贪婪模式会优先匹配最长的子串
converted_result = re.sub(r"01{1,4}", convert_match, binary_content)

print(converted_result)

关键说明:

  • 正则r"01{1,4}"表示匹配0后面跟1到4个1的子串,贪婪模式下会优先匹配更长的串(比如先匹配01111而不是拆成0111+1),完美契合你的转换优先级。
  • 替换函数根据匹配到的子串长度,生成对应数量的0结尾加1,直接完成转换。

方法二:手动遍历字符串(适合理解底层逻辑)

如果想更直观地理解转换过程,可以通过逐字符遍历的方式处理:

# 读取二进制文件内容
with open("binary_data.txt", "r") as f:
    binary_content = f.read().strip()

result_list = []
index = 0
total_length = len(binary_content)

while index < total_length:
    # 检查当前位置是否是规则的起始(0后面跟1)
    if (binary_content[index] == "0" 
        and index + 1 < total_length 
        and binary_content[index + 1] == "1"):
        # 统计后面连续的1的数量(最多4个,对应规则的最长串)
        one_count = 1
        while (index + 1 + one_count < total_length 
               and binary_content[index + 1 + one_count] == "1" 
               and one_count < 4):
            one_count += 1
        # 生成转换后的字符串并加入结果
        result_list.append("0" + "0" * one_count + "1")
        # 跳过已经处理的字符
        index += 1 + one_count
    else:
        # 非规则字符直接加入结果
        result_list.append(binary_content[index])
        index += 1

# 拼接结果列表为最终字符串
converted_result = "".join(result_list)
print(converted_result)

关键说明:

  • 逐个检查字符,当遇到0后跟1的组合时,统计后续连续1的数量(最多4个),再生成对应转换串。
  • 非规则的字符(比如单独的0、1,或者超过4个1的组合)直接保留,不做处理。

内容的提问来源于stack exchange,提问作者Rashid Ansari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:52:04