久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python編寫二進制

Python writing binary(Python編寫二進制)
本文介紹了Python編寫二進制的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我使用 python 3我嘗試將二進制文件寫入我使用 r+b 的文件.

I use python 3 I tried to write binary to file I use r+b.

for bit in binary:
    fileout.write(bit)

其中 binary 是一個包含數字的列表.如何將其寫入二進制文件?

where binary is a list that contain numbers. How do I write this to file in binary?

最終文件必須看起來像b' x07x08x07

The end file have to look like b' x07x08x07

謝謝

推薦答案

當您以二進制模式打開文件時,您實際上是在使用 bytes 類型.因此,當您寫入文件時,您需要傳遞一個 bytes 對象,而當您從中讀取時,您會得到一個 bytes 對象.相反,當以文本模式打開文件時,您正在使用 str 對象.

When you open a file in binary mode, then you are essentially working with the bytes type. So when you write to the file, you need to pass a bytes object, and when you read from it, you get a bytes object. In contrast, when opening the file in text mode, you are working with str objects.

所以,寫二進制"其實就是寫字節串:

So, writing "binary" is really writing a bytes string:

with open(fileName, 'br+') as f:
    f.write(b'x07x08x07')

如果你有實際的整數想要寫成二進制,你可以使用 bytes 函數將整數序列轉換為字節對象:

If you have actual integers you want to write as binary, you can use the bytes function to convert a sequence of integers into a bytes object:

>>> lst = [7, 8, 7]
>>> bytes(lst)
b'x07x08x07'

結合這一點,您可以將整數序列作為字節對象寫入以二進制模式打開的文件中.

Combining this, you can write a sequence of integers as a bytes object into a file opened in binary mode.

正如 Hyperboreus 在評論中指出的那樣,bytes 只會接受實際上適合一個字節的數字序列,即 0 到 255 之間的數字.如果你想存儲任意(正)整數以它們的方式,不必費心知道它們的確切大小(這是結構所必需的),然后您可以輕松編寫一個輔助函數,將這些數字分成單獨的字節:

As Hyperboreus pointed out in the comments, bytes will only accept a sequence of numbers that actually fit in a byte, i.e. numbers between 0 and 255. If you want to store arbitrary (positive) integers in the way they are, without having to bother about knowing their exact size (which is required for struct), then you can easily write a helper function which splits those numbers up into separate bytes:

def splitNumber (num):
    lst = []
    while num > 0:
        lst.append(num & 0xFF)
        num >>= 8
    return lst[::-1]

bytes(splitNumber(12345678901234567890))
# b'xabTxa9x8cxebx1f
xd2'

因此,如果您有一個數字列表,則可以輕松地遍歷它們并將每個數字寫入文件;如果您想稍后單獨提取數字,您可能需要添加一些東西來跟蹤哪些單個字節屬于哪些數字.

So if you have a list of numbers, you can easily iterate over them and write each into the file; if you want to extract the numbers individually later you probably want to add something that keeps track of which individual bytes belong to which numbers.

with open(fileName, 'br+') as f:
    for number in numbers:
        f.write(bytes(splitNumber(number)))

這篇關于Python編寫二進制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品一区二区欧美黑人喷潮水 | 狠狠影院| 午夜伦4480yy私人影院 | 中文字幕一区二区在线观看 | 国产欧美精品一区二区色综合朱莉 | 水蜜桃久久夜色精品一区 | 欧美一区二区三区在线看 | 国产精品午夜电影 | 国产精品视频网站 | 蜜桃在线一区二区三区 | 91成人免费看片 | 国产精品免费一区二区 | 久久精品国产一区二区三区 | 成人在线观看网站 | 成人综合在线视频 | 国产亚洲一区二区三区在线观看 | www.亚洲.com | 成人午夜电影网 | 国内精品视频在线观看 | 色五月激情五月 | 国产精品一区二区不卡 | 色偷偷噜噜噜亚洲男人 | 久久国产综合 | 免费xxxx大片国产在线 | 国产欧美精品一区二区色综合 | 欧美一区二区在线观看 | 一区二区精品电影 | 人人射人人插 | 九九热在线观看 | 一级一级毛片免费看 | 国产精品久久二区 | 日韩免| av影音资源 | 国产精品久久久久久亚洲调教 | 国产精品视频一区二区三区 | 中文字幕在线精品 | 国产成人精品一区二区三 | 久久免费电影 | 九九天堂网| 欧美视频三区 | 国产一级在线 |