python 保存数组和字符串

python 保存数组和字符串

更新了保存和读取多维数组(narray),保存成.npz格式
2020.09.19


如果只保存数组是很简单的,但是同时保存数组和字符串有些API用不了,奇怪,也是自己水平不够,解决不了。

先上代码(保存数组和字符串):
我将两个数组和一个维数相同的字符串拼接在一起,然后保存起来

y1=['yes','n1','n2','n3','yes1']
y1 = np.array(y1)
y1 = y1.reshape(5,1)
#print(y1.shape)
x = np.random.rand(5,10)
x1 = np.random.rand(5,10)
z1 = np.concatenate((x1,y1,x),axis=1)
z1 = np.array(z1)
np.save("/home/will/face_train",z1)

保存的格式为.npy,在文件名后面加不加.npy都一样,不加就自动补上。

其实一开始想用np.savetxt('PATH',a)保存成.txt格式的,但是会报错,字符串类型的问题,估计是因为含有两种类型的数据,如下:

TypeError: Mismatch between array dtype ('<U32') and format specifier

没有找到解决方法 TAT,如果哪位路过的大神,知道的,忘告知,当然不用np.savetxt()np.save()也是没问题的,读取.npy文件的代码如下:

A=np.load('/home/will/face_train.npy')
print('A.shape:',A.shape)

在这里就要记得加上文件的类型.npy,不然打不开的
output:

(5, 21)

2020.09.19更新部分

保存和读取多维数组——narray

np.savez('file_name',data),还可以保存多个多维数组np.savez('file_name',data1,data2,data3)

For example:

data1=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
data2=np.array([1, 2, 3], [4, 5, 6])
data3=np.array([1, 2, 3])
np.savez('test_data', data1, data2, data3)

读取用np.load('file_name.npz')
注意:读取完还查看不了。

  1. 需要先用
data = np.load('file_name.npz')
data.files # 查看保存了多少组不同的数组

得到输出:
保存一个多维数组:

['arr_0']

保存三个多维数组:

['arr_0','arr_1','arr_2']
  1. 之后再通过上面得到的索引号 索引对应数组:
da = data['arr_0']

For example:

data = np.load('test_data.npz')
data.files
# ['arr_0','arr_1','arr_2']
data['arr_0']
# array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
data['arr_1']
# array([1, 2, 3], [4, 5, 6])
data['arr_2']
# array([1, 2, 3])
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用join()函数将数组转换为字符串。join()函数是字符串的一个方法,它将数组中的元素连接成一个字符串,并使用指定的分隔符将它们分隔开来。你可以通过在数组名称之前使用指定的分隔符来调用join()函数。 例如,如果我们有一个名为arr的数组,我们可以使用以下代码将其转换为字符串: ``` arr = ['hello', 'world', 'python'] separator = ' ' string = separator.join(arr) print(string) # 输出: hello world python ``` 在这个例子中,我们将数组arr中的元素使用空格作为分隔符连接起来,然后将结果保存字符串变量string中。最后,我们使用print()函数将结果打印出来。请注意,join()函数只能用于字符串数组,如果数组中存在非字符串元素,则需要先将其转换为字符串。 引用提供了split()方法的使用,该方法用于将字符串分割成数组。然而,你的问题是将数组转换为字符串,因此split()方法并不适用于这种情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python数组字符串互转详细操作](https://blog.csdn.net/mycocoo/article/details/115213445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python数组字符串之间的转换](https://blog.csdn.net/myself360013139/article/details/110221155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值