将多个numpy数组写入文件 [英] Write multiple numpy arrays to file

查看:1129
本文介绍了将多个numpy数组写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用numpy.savetxt将数组写入文件。如何将多个数组写入同一个文件?

I know how to use numpy.savetxt to write an array to a file. How can I write multiple arrays to the same file?

基本上我想对一列数字进行数学运算,然后用修改后的数字替换旧列。我读到最简单的方法是完全写一个新文件,将修改后的数字放入,然后复制并粘贴文件中的其他数字。

Essentially I want to do math to a column of numbers, and then replace the old column with the modified numbers. I read the easiest way to do this is to write a new file completely, put the modified numbers in, and just 'copy and paste' the other numbers in the file.

感谢任何帮助。

谢谢!

推荐答案

如果您想将多个数组写入文件供以后使用,请查看 numpy.savez

If you're wanting to write multiple arrays to a file for later use, Look into numpy.savez.

但是,根据你的描述,它听起来像你'我想要对分隔文本文件的特定列做一些事情。

However, from your description, it sounds like you're wanting to do something with a particular column of a delimited text file.

在这种情况下,只需加载整个内容并操作您需要的列。

In that case, just load the entire thing in and operate on just the column you need to.

例如

import numpy as np

data = np.loadtxt('test.txt')

# Multiply the 4th column by 5
data[:,3] *= 5

# Do something more complicated to the 2nd column
data[:,1] = np.cos(data[:,1])

# Save the array back to the file
np.savetxt('test.txt', data)

这篇关于将多个numpy数组写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆