python實現(xiàn)在內(nèi)存中讀寫str和二進制數(shù)據(jù)代碼
更新時間:2020年04月24日 10:46:05 作者:xiao_xia_ming
這篇文章主要介紹了python實現(xiàn)在內(nèi)存中讀寫str和二進制數(shù)據(jù)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,還是直接看代碼吧!
# 利用python在內(nèi)存中讀寫str和二進制數(shù)據(jù)
from io import StringIO
from io import BytesIO
f = StringIO()
print(f.write('hello ')) # 6
print(f.write('world!')) # 6
print(f.getvalue()) # hello world!
f = BytesIO()
print(f.write('中文'.encode('utf-8'))) # 6
print(f.getvalue()) # b'\xe4\xb8\xad\xe6\x96\x87'
補充知識:python二進制轉(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:\Users\xuguanghui\Desktop\106421_mlplib.lab" train_label = r"C:\Users\xuguanghui\Desktop\106421_train.lab" mlplib_txt = r"C:\Users\xuguanghui\Desktop\106421_mlplib.txt" train_txt = r"C:\Users\xuguanghui\Desktop\106421_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實現(xiàn)在內(nèi)存中讀寫str和二進制數(shù)據(jù)代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)
本文是小編日常整理的些關(guān)于python執(zhí)行系統(tǒng)命令常見的方法,比較全面,特此通過腳本之家這個平臺把此篇文章分享給大家供大家參考2015-10-10
pandas實現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式
這篇文章主要介紹了pandas實現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點
Python函數(shù)的默認值參數(shù)只會在函數(shù)定義處被解析一次,以后再使用時這個默認值還是一樣,這在與可變參數(shù)共同使用時便會產(chǎn)生困惑,下面就來小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點2016-06-06

