简体   繁体   中英

Write multiple Arrays, of various sizes, into a text file

I have the following issue:

I need to write a given number of Arrays, which all have the same number of rows, but a different number of columns into a text file. Is there another way other than checking for each array, which dimesnion it has and then writing each entry into the text file?

I tried something like this:

def WriteResultToTxt(X,Y):

    f = open('Test.txt','w')
    Z = zip(X,Y)
    np.savetxt(f,Z)
    f.close

and when I try this:

x = np.arange(0,200,1).reshape((100,2))
y = np.arange(0,50,0.5).reshape((100,1))
WriteResultToTxt(x,y)

I get an 'tuple index out of range' error

Easiest would be to use Pandas.

import pandas as pd
import numpy as np
x = np.arange(0,200,1).reshape((100,2))
y = np.arange(0,50,0.5).reshape((100,1))
pd.concat([pd.DataFrame(x), pd.DataFrame(y)], axis=1).to_csv('foo.csv')

First concatenate the two arrays and then export to CSV. By specifying axis=1 the concatenation would be column-wise; one next to the other and not row-wise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM