【问题标题】:How to write different array together to a file如何将不同的数组一起写入文件
【发布时间】:2019-12-04 22:15:35
【问题描述】:

我想将某个hdf5文件中的一些信息写入一个新的txt文件。 信息包括时间,纬度,经度,obs,协方差矩阵,其中每个变量都是数组。

time是一维数组,形状为(t,)latlon也是一维数组,形状为(t,),表示特定时间的位置,obs是二维数组,形状为(t,n),表示我们有t个观察值,每个观察值包含n个状态,covariance matrix是一个3D数组,形状为(t,n,n)。我想做什么一一提取这些信息。

with open('test.dat','w') as output_file:
    for i in np.arange(10):
        output_file.writelines(str(time[i])+' '+str(lat[i])+' '+str(lon[i])+'\n')
        output_file.writelines('%s ' % l for l in obs[i])
        output_file.write('\n')
        output_file.writelines('%s ' % l for l in cov[i])
        output_file.write('\n')

假设我们有time= np.array([1,2,3])lat=np.array([20,30,40]),lon=np.array([40,50,70]), obs=np.random.random((3,4)),cov = np.random.random((3,4,4)), 我希望输出文件这样写:

time[0] lat[0] lon[0]
obs[0,0] obs[0,1] obs[0,2] obs[0,3] 
cov[0,0,0] cov[0,0,1] cov[0,0,2] cov[0,0,3]
....
....
cov[0,3,0] cov[0,3,1] cov[0,3,2] cov[0,3,3]
time[1] lat[1] lon[1]
.....

示例输出中的每一行都是输出文件中的一个文本行。一行中的字段由一个空格分隔。 但是我写的代码好像不行,怎么办?

【问题讨论】:

  • 您是否尝试使用 HDF5?如果您执行output_file.write('\n'),您将编写一个文本文件。我希望你想要 h5py 并拥有类似 with h5py.File('test.h5') as f: ... 的东西,而不是仅仅在文件上使用 open
  • @RoryDaulton,对不起,我已经更正了。
  • @ScottStaniewicz,不,我想将不同的数组从 hdf5 放到一个新的 txt 文件中。
  • 注意:str[time[i]] 将是无效的语法,您需要str(time[i])

标签: python file numpy


【解决方案1】:

此代码似乎有效。我通过使用print(*anarray) 技巧避免了一些循环,以便在一行中轻松打印一维数组的内容,并用空格分隔。我使用print 而不是write 来轻松地将行尾标记放在正确的位置。我本可以省略 sep=' ' 参数,因为它们是默认参数,但我认为“显式优于隐式”。我删除了您的 str 转换,因为 print 会自动进行该转换。我还添加了一些assert 语句来保证数组形状的一致性。

import numpy as np

t = 3
n = 4

time = np.array([1, 2, 3])
lat = np.array([20, 30, 40])
lon = np.array([40, 50, 70])
obs = np.random.random((3, 4))
cov = np.random.random((3, 4, 4))

assert time.shape == lat.shape == lon.shape == (t,)
assert obs.shape == (t, n)
assert cov.shape == (t, n, n)

with open('test.dat', 'w') as output_file:
    for i in range(t):
        print(time[i], lat[i], lon[i], sep=' ', file=output_file)
        print(*obs[i], sep=' ', file=output_file)
        for j in range(n):
            print(*cov[i, j], sep=' ', file=output_file)

这是代码运行后文件test.dat的内容:

1 20 40
0.3299447219312376 0.996996139803863 0.9551526837239497 0.5509582503806248
0.8819189869595838 0.9132355846068761 0.9665353304879704 0.46635220924415366
0.5181587118344747 0.8038185825031468 0.6745351776394765 0.5995935654205414
0.8619757623942051 0.482577455518059 0.7957648986891931 0.7407148158441567
0.09795020526664455 0.794707302185401 0.5293100793000466 0.3338543543683751
2 30 50
0.8035406182773407 0.1185746504181443 0.580297768900217 0.18777926684714497
0.532529800187838 0.12522453075786388 0.46300433798756324 0.22510518678645997
0.29260759039257944 0.3045692764639927 0.8085267868489262 0.6155070434401326
0.744762931777478 0.3722679713441319 0.395290426225012 0.3838002215325691
0.8049013701055735 0.4221372661820496 0.9607451508389756 0.7143917783338681
3 40 70
0.747699155972642 0.0016320243955539881 0.6063364532047132 0.8264067710516358
0.5795303829414947 0.49210530688638277 0.7499924344997647 0.601564975827258
0.7578052140931559 0.3514264858000006 0.7016988143157379 0.7628124804393464
0.3503877412670806 0.4020137310035564 0.688387279488728 0.46935179605446187
0.21964712005015719 0.8918053594781274 0.7908691508919491 0.006491011595309404

【讨论】:

  • 我们可以在使用print(*anarray)技巧时以格式化的方式打印这些值,例如在%e
  • @AllenZhang:鉴于the print function 的工作方式,我看不到在使用print(*anarray) 时如何格式化项目。为此需要一个循环。循环可能隐藏在列表推导中,以生成格式化字符串列表而不是浮点数列表。
猜你喜欢
  • 2012-01-29
  • 1970-01-01
  • 2017-07-09
  • 2017-08-31
  • 2012-11-22
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多