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

將二進制整數或字符串寫入python中的文件

Write a binary integer or string to a file in python(將二進制整數或字符串寫入python中的文件)
本文介紹了將二進制整數或字符串寫入python中的文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 Python 中有一個字符串(也可以是整數),我想將它寫入文件.它只包含 1 和 0 我希望將這種 1 和 0 的模式寫入文件.我想直接編寫二進制文件,因為我需要存儲大量數據,但只存儲某些值.當我只需要三個時,我認為沒有必要占用每個值使用八位的空間.

I have a string (it could be an integer too) in Python and I want to write it to a file. It contains only ones and zeros I want that pattern of ones and zeros to be written to a file. I want to write the binary directly because I need to store a lot of data, but only certain values. I see no need to take up the space of using eight bit per value when I only need three.

例如.假設我要將二進制字符串 "01100010" 寫入文件.如果我在文本編輯器中打開它,它會顯示 b(01100010 是 b 的 ascii 代碼).不過不要混淆.我不想寫ascii代碼,這個例子只是為了表明我想直接將字節寫入文件.

For instance. Let's say I were to write the binary string "01100010" to a file. If I opened it in a text editor it would say b (01100010 is the ascii code for b). Do not be confused though. I do not want to write ascii codes, the example was just to indicate that I want to directly write bytes to the file.

澄清:

我的字符串看起來像這樣:

My string looks something like this:

binary_string = "001011010110000010010"

它不是由數字或字符的二進制代碼組成的.它包含僅與我的程序相關的數據.

It is not made of of the binary codes for numbers or characters. It contains data relative only to my program.

推薦答案

好吧,經過一番搜索,我找到了答案.我相信你們其他人根本不明白(這可能是我的錯,因為我不得不編輯兩次才能說清楚).我在這里找到了它.

Alright, after quite a bit more searching, I found an answer. I believe that the rest of you simply didn't understand (which was probably my fault, as I had to edit twice to make it clear). I found it here.

答案是將每條數據拆分,將它們轉換為二進制整數,然后將它們放入二進制數組中.之后,您可以使用數組的 tofile() 方法寫入文件.

The answer was to split up each piece of data, convert them into a binary integer then put them in a binary array. After that, you can use the array's tofile() method to write to a file.

from array import *

bin_array = array('B')

bin_array.append(int('011',2))
bin_array.append(int('010',2))
bin_array.append(int('110',2))

with file('binary.mydata', 'wb') as f:
    bin_array.tofile(f)

這篇關于將二進制整數或字符串寫入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毛片 | 亚洲区一区二区 | 久久草视频| 色视频网站| 日韩欧美精品 | 国产区一区二区三区 | 艹逼网 | 欧美一级黄色免费 | 爱综合| 久久久国产一区二区三区 | 中文字幕在线视频一区二区三区 | 99精品电影| 国产成人在线一区二区 | 欧美一级www片免费观看 | 日韩欧美一区二区三区免费观看 | 日韩国产一区二区三区 | 成人综合视频在线观看 | 亚洲另类视频 | 中文字幕一区二区三区精彩视频 | 一区二区三区四区在线免费观看 | 五月花丁香婷婷 | 国产一二三视频在线观看 | 亚洲成人观看 | 成人免费视频在线观看 | 欧美日韩精品专区 | 久久久久久久电影 | 日韩成人免费av | 国产免费观看视频 | 欧美理论在线观看 | 一区二区三区视频 | 成人欧美一区二区三区白人 | 国产激情在线观看 | 欧美精品久久久久 | av一区二区三区四区 | 91国产在线视频在线 | 91亚洲一区| 国产性网 | 日韩精品久久 | 日韩喷潮 | 一级免费毛片 |