python使用numpy读取、保存txt数据的实例
作者:AManFromEarth 时间:2021-04-05 22:38:49
1.首先生成array数组
import numpy as np
a = np.random.rand(5,5)
print(a)
结果:
array([[0.17374613, 0.87715267, 0.93111376, 0.53415215, 0.59667207],
[0.6865835 , 0.15873242, 0.2842251 , 0.73840834, 0.37163279],
[0.06556834, 0.68446787, 0.91136611, 0.82796704, 0.81343561],
[0.99336674, 0.22961447, 0.78337783, 0.12448455, 0.04388831],
[0.50053951, 0.046609 , 0.98179001, 0.446681 , 0.68448799]])
2.保存至txt
使用numpy.savetxt函数,文档在这里:
https://docs.scipy.org/doc/numpy-1.14.2/reference/generated/numpy.savetxt.html#numpy.savetxt
np.savetxt('a.txt',a,fmt='%0.8f')
#第一个参数是要保存的文件名
#第二参数是要保存的array
#第三个参数是保存的数据格式,详见文档
结果:
3.从txt文件中读取数据
b=np.loadtxt('a.txt',dtype=np.float32)
print(b)
结果:
array([[0.17374612, 0.8771527 , 0.9311138 , 0.53415215, 0.59667206],
[0.6865835 , 0.15873241, 0.2842251 , 0.7384083 , 0.37163278],
[0.06556834, 0.68446785, 0.9113661 , 0.82796705, 0.8134356 ],
[0.9933667 , 0.22961447, 0.7833778 , 0.12448455, 0.04388831],
[0.5005395 , 0.046609 , 0.98179 , 0.446681 , 0.684488 ]],
dtype=float32)
来源:https://blog.csdn.net/AManFromEarth/article/details/80259649
标签:python,numpy,txt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
再谈javascript图片预加载技术
2011-03-11 19:57:00
Oracle数据库的安全策略
2010-07-31 13:13:00
教你如何利用SQL Server保护数据
2010-06-07 14:18:00
xmlHTTP技术资料
2008-01-05 13:39:00
详细了解 MySQL锁机制
2010-08-08 09:04:00
python排序算法之选择排序
2023-03-15 11:56:56
![](https://img.aspxhome.com/file/2023/2/65112_0s.png)
asp如何用WSH获取机器的IP配置信息?
2010-06-13 14:39:00
Django应用程序中如何发送电子邮件详解
2023-11-04 02:07:54
![](https://img.aspxhome.com/file/2023/2/65452_0s.png)
用python实现五子棋实例
2022-08-23 21:42:38
![](https://img.aspxhome.com/file/2023/8/65908_0s.jpg)
python实现指定文件夹下的指定文件移动到指定位置
2023-07-03 08:21:11
WPF自定义搜索框代码分享
2023-07-18 23:31:04
![](https://img.aspxhome.com/file/2023/1/79061_0s.jpg)
Oracle REGEXP_LIKE模糊查询用法例子
2023-07-19 08:26:22
![](https://img.aspxhome.com/file/2023/8/64448_0s.png)
很有意思的SQL多行数据拼接
2011-11-03 17:08:29
ASP访问SQL Server内置对象
2008-04-05 06:49:00
Oracle 数据 使用游标
2009-07-02 12:14:00
与 Function 和 Object 相关的有趣代码
2010-05-07 12:29:00
![](https://img.aspxhome.com/file/UploadPic/20105/7/javascript_object_layout-52s.jpg)
保护Access 2000数据库的安全
2008-10-23 13:55:00
SQL SERVER如何判断某个字段包含大写字母
2023-07-01 21:19:12
使用一条INSERT语句完成多表插入
2010-03-18 11:08:00
php实现图片转换成ASCII码的方法
2023-09-07 12:10:57