python實現在內存中讀寫str和二進制數據代碼
我就廢話不多說了,還是直接看代碼吧!
# 利用python在內存中讀寫str和二進制數據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’
補充知識:python二進制轉到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實現在內存中讀寫str和二進制數據代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: