plethora
plethora

Reputation: 135

How to save and read back a multidimensional string array (possibly) with numpy?

I need to save data to a file where each line follows this format: <string1> <array of thousands of floats> <string2>. So I thought about concatenating the data into one huge string array, as below:

labels = ['label1', 'label2', 'label3']
values = [[0.1, 0.4, 0.5],
          [0.1, 0.2, 0.1],
          [0.5, 0.6, 1.0]]
descriptions = ['desc1', 'desc2', 'desc3']
concat1 = np.r_['1,2,0', labels, values]
concat2 = np.r_['1,2,0', concat1, descriptions]

Result:

[['label1' '0.1' '0.4' '0.5' 'desc1']
 ['label2' '0.1' '0.2' '0.1' 'desc2']
 ['label3' '0.5' '0.6' '1.0' 'desc3']]

I know that if each subarray were small enough I could do something like this:

np.savetxt('output.txt', concat2, fmt = "%s %s %s %s %s")

But my problem involves thousands of values, so it's kind of impractical to type the format one variable at a time.

Any other suggestion of how to save it to file?

PS: It sounds a bit weird to save floats as strings, but my superior asked it like this, so...

Upvotes: 1

Views: 88

Answers (1)

Heiko Oberdiek
Heiko Oberdiek

Reputation: 1708

A solution without numpy:

labels = ['label1', 'label2', 'label3']
values = [[0.1, 0.4, 0.5],
          [0.1, 0.2, 0.1],
          [0.5, 0.6, 1.0]]
descriptions = ['desc1', 'desc2', 'desc3']

with open('output.txt', 'w') as handle:
    for label, nums, description in zip(labels, values, descriptions):
        handle.write('{} {} {}\n'.format(
            label,
            ' '.join(map(str, nums)),
            description,
        ))

Contents of output.txt:

label1 0.1 0.4 0.5 desc1
label2 0.1 0.2 0.1 desc2
label3 0.5 0.6 1.0 desc3

Or starting from concat2:

with open('output.txt', 'w') as handle:
    for row in concat2:
        handle.write(' '.join(row))
        handle.write('\n')

Upvotes: 1

Related Questions