如何将表示为字符串的二进制数拆分为子字符串 [英] How to split a binary number represented as a string to sub strings

查看:18
本文介绍了如何将表示为字符串的二进制数拆分为子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个二进制文件.并希望将一个二进制数拆分为更小的部分.

例如一个输入数字 01001100001011000100110000101100010011000 应该被解析为 0 1 00 11 0000 1 0 11 000 1 00 0 1 0 0 code 0 0 1 0 0 1 0/p>

这是我迄今为止尝试过的吗?

 from tkinter import filedialogfile_path = filedialog.askopenfilename()使用 open(file_path, 'rb') 作为 f:a = f.read打印(a.replaceint(01",0 1"))打印(a.replaceint(10",1 0"))打印(f,文件=打开(space.txt",a"))

解决方案

这应该可行,如果您有任何问题,请告诉我.

import itertoolss = 01001100001011000100110000101100010011000"对于 itertools.groupby(s) 中的 k,v:打印(k*len(列表(v)))

I am working with a binary file. and want to split a binary number to smaller parts.

For example an input number 01001100001011000100110000101100010011000 should be parsed to 0 1 00 11 0000 1 0 11 000 1 00 11 0000 1 0 11 000 1 00 11 000.

This is what I have tried so far?

    from tkinter import filedialog
    
    file_path = filedialog.askopenfilename()
    
    with open(file_path, 'rb') as f:
        a = f.read
    
    print(a.replaceint("01", "0 1"))
    print(a.replaceint("10", "1 0"))
       
    print(f, file=open("space.txt", "a"))

解决方案

This should work, let me know if you have any questions.

import itertools

s = "01001100001011000100110000101100010011000"

for k,v in itertools.groupby(s):
    print(k*len(list(v)))

这篇关于如何将表示为字符串的二进制数拆分为子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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