Python中文
首页
教程
问答
标签
搜索
登录
注册
如何在文本文件中编写numpy矩阵-python
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>假设我从计算中得到一个numpy矩阵。这是我的numpy矩阵“result1”:</p> <pre><code> result1= [[ 1. 0. 0. 0.00375 -0.01072 -0. -1000. ] [ 2. 3. 4. 0. -0.004 750. 0. ] [ 3. 3. 0. 0. 0. -750. 1000. ]] </code></pre> <p>现在我想把这个矩阵写在一个名为'result.txt'的文本文件中。为此,我编写了以下代码:</p> <pre><code>np.savetxt('result.txt', result1, fmt='%.2e') </code></pre> <p>但它把矩阵的所有元素都放在一行。</p> <pre><code> 1.00e+00 0.00e+00 0.00e+00 3.75e-03 -1.07e-02 -1.14e-13 -1.00e+032.00e+00 3.00e+00 4.00e+00 0.00e+00 -4.00e-03 7.50e+02 0.00e+003.00e+00 3.00e+00 0.00e+00 0.00e+00 0.00e+00 -7.50e+02 1.00e+03 </code></pre> <p>我想以适当的矩阵格式在文本文件中写入矩阵。我该怎么做?我使用关键字newline='\n'或newline='',但结果相同。</p> <p>提前谢谢。。。</p> <p>=</p> <p>这个编辑部分是给@Warren的</p> <p>试试这个:</p> <pre><code>>>> import numpy as np >>> mat=np.matrix([[1, 2, 3],[4, 5, 6],[7, 8, 9]]) >>> mat matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> np.savetxt('text.txt',mat,fmt='%.2f') </code></pre> <p>在我的text.txt文件中,我得到:</p> <p>1.00 2.00 3.004.00 5.00 6.007.00 8.00 9.00</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>就像Francesco Nazzaro的回答,但是要确保文件能够成功打开,请尝试:</p> <pre><code>a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) mat = np.matrix(a) with open('outfile.txt','wb') as f: for line in mat: np.savetxt(f, line, fmt='%.2f') </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
无法使用Django/mongoengine连接到MongoDB(身份验证失败)
7 回答
无法使用Django\u mssql\u后端迁移到外部hos
8 回答
无法使用Django&Python3.4连接到MySql
5 回答
无法使用Django+nginx上载媒体文件
5 回答
无法使用Django1.6导入名称模式
5 回答
无法使用Django1.7和mongodb登录管理站点
7 回答
无法使用Djangoadmin创建项目,进程使用了错误的路径,因为我事先安装了错误的Python
2 回答
无法使用Djangockedi验证CBV中的字段
1 回答
无法使用Djangocketditor上载图像(错误400)
4 回答
无法使用Djangocron进行函数调用
5 回答
无法使用Djangofiler djang上载文件
10 回答
无法使用Djangokronos
2 回答
无法使用Djangomssql provid
4 回答
无法使用Djangomssql连接到带有Django 1.11的MS SQL Server 2016
7 回答
无法使用Djangomssq迁移Django数据库
7 回答
无法使用Djangonox创建用户
9 回答
无法使用Djangopyodb从Django查询SQL Server
4 回答
无法使用Djangopython3ldap连接到ldap
5 回答
无法使用Djangoredis连接到redis
4 回答
无法使用Django中的FK创建新表
2 回答