簡體   English   中英

使用open()從URL獲得BytesIO

[英]BytesIO from url using open()

您能幫我嗎,我需要在rb模式下打開my_url 嘗試這樣做。

 url = "https://my url/" + file_info.file_path
            response = requests.get(url)

            with open(BytesIO(response.content), "rb") as f:  # Open in 'rb' mode for reading it in way like: 010101010
                byte = f.read(1) 
                #some algorithm..............
                while byte:
                    hexadecimal = binascii.hexlify(byte)
                    decimal = int(hexadecimal, 16)
                    binary = bin(decimal)[2:].zfill(8)
                    hiddenData += binary
                byte = f.read(1)

有錯誤:

預期的str,bytes或.osPathLIke對象,而不是_ioBytesIO

請您提供幫助,如何在"rb"模式下打開我的網址?

我試圖使用Pillow打開圖像-可以。 但是對於使用open() ,我不能做同樣的事情。 請..

您正在傳遞一個BytesIO文件名的BytesIO對象(基本上是文件句柄)。

如此快速修復:

f = BytesIO(response.content) 

但更好的方法是,手動(用於算法的開始)或自動(使用for循環,當迭代器用盡時將停止,因此不需要使用while )對iter進行bytes對象的迭代:

f = iter(response.content)

byte = next(f)

#some algorithm..............
for byte in f:
    hexadecimal = binascii.hexlify(byte)
    decimal = int(hexadecimal, 16)
    binary = bin(decimal)[2:].zfill(8)
    hiddenData += binary

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM