【掌控板】7.3、数组转化及存储前⾔
将整形数组转换为字符型数组
需要⽤到insert,提取数组中对应序列号的数值
然后使⽤之前写的int转char函数
同时新增字符型数组存储到txt⽂件中
代码1_字符型数组存储到txt⽂件
参考之前写的
#---------------------------------------------
# func8: 数组转化及存储
#
#---------------------------------------------
import os
path="test\\c.txt"#define file path
xxx =open(path,"a")
print(type(xxx))数组转换成字符串
sound_list=['12','123','1222']
print(sound_list)
print(type(sound_list))
xxx.write('sound_list\n')
#xxx.write(sound_list)
#file_mic.write("\n")
#print(ad())
print('write ok')
xxx.close()
结果1
报错,因为write需要输⼊的是字符串类型
参见
file.write(“hello\n”) #如果要写⼊字符串以外的数据,先将他转换为字符串.
所以直接将list存到⽂件中⾏不通,需要先转换为字符串型
代码2——int型转字符型数组
#---------------------------------------------
# func9: 数组转化及存储(int型转字符型数组)
#
#
#---------------------------------------------# MindPlus
# mpython
import os
# ⼗进制数字转换为字符
def int2char(x):
numbers ={
0:'0',
1:'1',
2:'2',
3:'3',
4:'4',
5:'5',
6:'6',
7:'7',
8:'8',
9:'9',
10:'a',
11:'b',
12:'c',
13:'d',
14:'e',
15:'f',
}
(x,None)
# ⼗进制int转⼗进制字符串
def int_2_dec( value , key_num):
a=value%key_num
result=''
#print("value",value)
if(a ==0)&(value ==0):
result ='0'
return result
#print("result",result)
#print("a!=0",a!=0)
#print("value > 0",value > 0) while True:
if a ==0:
if value <=0:
break
#print("enter")
#print("a",a)
temp=int2char(a)
#temp=hex(a).replace('0x','')
result=temp+result
#print("result",result)
value=(value-a)//key_num
#print("value",value)
a=value%key_num
#print("a",a)
return result
def intlist_2_char(intlist):
list_len=len(intlist)
char=''
print("list_len",list_len)
#1index=0
for index in range(list_len):
print("index",index)
temp=int_2_dec(intlist[index],10) print("temp",temp)
print("temp",temp)
char=char+'\n'+temp
print(char)
return char
path="test\\c.txt"#define file path
xxx =open(path,"a")
print(type(xxx))
sound_list=[1,2,3]
print(sound_list)
print(type(sound_list))
charlist=intlist_2_char(sound_list)
xxx.write('sound_list')
xxx.write(charlist)
#file_mic.write("\n")
#print(ad())
print('write ok')
xxx.close()
结果2
代码3——整合⾳量获取
#---------------------------------------------
# func10: 尝试利⽤数组将采样的数值进⾏存储(1s停⽌抓取数据)+数组转换#
#---------------------------------------------
import ntptime,network
from mpython import*
from machine import Timer
# int型数组转换为字符串
def intlist_2_char(intlist):
list_len=len(intlist)
char=''
#print("list_len",list_len)
#1index=0
for index in range(list_len):
#print("index",index)
temp=int_2_dec(intlist[index],10)
#print("temp",temp)
char=char+'\n'+temp
char=char+'\n'+temp
#print(char)
print("-----stop change-----")
return char
# ⼗进制数字转换为字符
def int2char(x):
numbers ={
0:'0',
1:'1',
2:'2',
3:'3',
4:'4',
5:'5',
6:'6',
7:'7',
8:'8',
9:'9',
10:'a',
11:'b',
12:'c',
13:'d',
14:'e',
15:'f',
}
(x,None)
# ⼗进制int转⼗进制字符串
def int_2_dec( value , key_num):
a=value%key_num
result=''
#print("value",value)
if(a ==0)&(value ==0):
result ='0'
return result
#print("result",result)
#print("a!=0",a!=0)
#print("value > 0",value > 0)
while True:
if a ==0:
if value <=0:
break
#print("enter")
#print("a",a)
temp=int2char(a)
#temp=hex(a).replace('0x','')
result=temp+result
#print("result",result)
value=(value-a)//key_num
#print("value",value)
a=value%key_num
#print("a",a)
return result
# 定义刷新函数,获取时间并显⽰
def Refresh(_):
print("-----in 1s-----")
global temp_num #定义全局变量global sound_list
global status
status=0
temp_value=int_2_dec(temp_num,10) print("temp_value",temp_value)
print("sound_list",len(sound_list))
print("sound_list",len(sound_list))
print("-----start change-----")
result_char=intlist_2_char(sound_list)
print("result_char",result_char)
sound_list=[]# 需要清空数组,否则会报错内存不够
temp_num=0
return
temp_num=0
sound_list=[]
status=1
# 定时器1
tim1 = Timer(1)
# 初始化定时器1,设置时间1s,Timer.PERIODIC周期性,回调函数Refresh tim1.init(period=1000, mode=Timer.ONE_SHOT, callback=Refresh)
while status :
temp_num=temp_num+1
sound_value_ad()
sound_list.append(sound_value_int)
# sound_value_char=int_2_dec(sound_value_int,10)
# print('sound',sound_value_char)
# oled.DispChar('sound', 0, 0, 1)
# oled.DispChar(sound_value_char, 0, 16, 1)
print("test end")
结果3
⽅法是ok的,但是我忘了⼀点,字符串存储⼜双叒叕超内存了。
打印index,查看最多能显⽰多少数据
所以尝试⼀次取2000个,⽤数组再存储吧。
代码4——基于代码2设置字符型数组存储

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。