久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 成人国产午夜在线观看 | 黄色片视频网站 | a级网站| 欧美精品久久 | 欧美日韩视频在线播放 | 日韩一区二区在线观看 | 红桃视频一区二区三区免费 | 亚洲精品第一页 | 欧美专区在线视频 | 色频| 午夜影视 | 黄色一级大片视频 | av三级| 国产精品免费在线 | 99这里只有精品视频 | 亚洲视频不卡 | 久久尤物免费一区二区三区 | 日本黄色不卡视频 | 一级一级毛片免费看 | 高清一区二区三区 | 免费一区 | 国产成人av一区二区三区 | 亚洲电影第1页 | 黑人精品xxx一区一二区 | 天天色天天射天天干 | 精品一区二区久久久久久久网精 | 午夜视频在线免费观看 | av一二三四 | 国产欧美在线播放 | 国产精品美女久久久久久免费 | 国产视频线观看永久免费 | 成人免费视频网址 | 成人av播放 | 成人在线视频一区二区三区 | 欧美一区二区在线播放 | 亚洲在线| 亚洲第一色站 | 日韩精品二区 | 亚洲综合色 | 成人在线观看免费视频 | 夜夜久久 |