Python get rid of bytes b' '

后端 未结 6 1452
悲&欢浪女
悲&欢浪女 2021-02-18 17:24
import save

string = \"\"

with open(\"image.jpg\", \"rb\") as f:
    byte = f.read(1)
    while byte != b\"\":
        byte = f.read(1)
        print ((byte))
<         


        
6条回答
  •  佛祖请我去吃肉
    2021-02-18 18:04

    To operate on binary data you can use the array-module. Below you will find an iterator that operates on 4096 chunks of data instead of reading everything into memory at ounce.

    import array
    
    def bytesfromfile(f):
        while True:
            raw = array.array('B')
            raw.fromstring(f.read(4096))
            if not raw:
                break
            yield raw
    
    with open("image.jpg", 'rb') as fd
        for byte in bytesfromfile(fd):
            for b in byte:
                # do something with b 
    

提交回复
热议问题