python實(shí)現(xiàn)在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)代碼
我就廢話不多說(shuō)了,還是直接看代碼吧!
# 利用python在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)from io import StringIOfrom io import BytesIO f = StringIO()print(f.write(’hello ’)) # 6print(f.write(’world!’)) # 6print(f.getvalue()) # hello world! f = BytesIO()print(f.write(’中文’.encode(’utf-8’))) # 6print(f.getvalue()) # b’xe4xb8xadxe6x96x87’
補(bǔ)充知識(shí):python二進(jìn)制轉(zhuǎn)到float
看代碼吧!
# -*- coding: utf-8 -*-'''Created on Tue Dec 3 14:38:04 2019@author: xuguanghui''' import numpy as np mlplib_label = r'C:UsersxuguanghuiDesktop106421_mlplib.lab'train_label = r'C:UsersxuguanghuiDesktop106421_train.lab'mlplib_txt = r'C:UsersxuguanghuiDesktop106421_mlplib.txt'train_txt = r'C:UsersxuguanghuiDesktop106421_train.txt' mlplib_lab = np.fromfile(mlplib_label, dtype=np.int32).reshape(-1, 892)train_lab = np.fromfile(train_label, dtype=np.float32).reshape(-1, 892) np.savetxt(mlplib_txt, mlplib_lab, fmt=’%d’)np.savetxt(train_txt, train_lab, fmt=’%d’)
以上這篇python實(shí)現(xiàn)在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. js實(shí)現(xiàn)碰撞檢測(cè)2. 用Spring JMS使異步消息變得簡(jiǎn)單3. JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁(yè)碼代碼實(shí)例4. 一文帶你徹底理解Java序列化和反序列化5. 關(guān)于IDEA 2020.3 多窗口視圖丟失的問(wèn)題6. PHP驗(yàn)證碼工具-Securimage7. PHP使用Swagger生成好看的API文檔8. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條9. 通過(guò)實(shí)例解析Python文件操作實(shí)現(xiàn)步驟10. Python 下載Bing壁紙的示例

網(wǎng)公網(wǎng)安備