1

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...

8
  • 1
    Have you tried numpy.ndarray.tofile? Commented Jun 17, 2017 at 23:44
  • 2
    fmt="%s" should work. savetxt` replicates that value to account for the number of values in the array row. That is, it constructs newfmt = delimiter.join([fmt]*len(row)). Commented Jun 18, 2017 at 0:27
  • @BradSolomon It works but then I can't read back into a multidimensional array. It's saved as a single line. Commented Jun 18, 2017 at 0:29
  • So you are talking about a csv file with thousands of columns. Also one that's a mix of string and float columns? How were you going to read it? With genfromtxt into a structured array? Save and read might be simpler if you saved the string data to one file, and floats to another. Commented Jun 18, 2017 at 0:41
  • 1
    stackoverflow.com/questions/36507283/… - a small example of saving a mix of string and numbers. Commented Jun 18, 2017 at 1:30

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.