简体   繁体   中英

combining python lists in savetxt

I have 2 python lists that I want to save side by side using np.savetxt().

I have tried:

np.savetxt('./filename.txt',np.hstack([list1,list2]),fmt=['%f','%f'])

but I get the error message

raise AttributeError('fmt has wrong shape.  %s' % str(fmt))
AttributeError: fmt has wrong shape.  ['%f', '%f']

I don't know if it is relevant, but the lists are in decimal.Decimal format.

What am I doing wrong please?


edit: I originally said "vstack" but I meant "hstack".

Just pass a single value to fmt , like this:

np.savetxt('./filename.txt',np.vstack([list1,list2]),fmt='%f')

Example:

import decimal, numpy as np
a = np.array([decimal.Decimal("1.0"),
              decimal.Decimal("2.0"),
              decimal.Decimal("3.0")],
             dtype=np.dtype(decimal.Decimal))
b = a + 1
np.savetxt('./filename.txt',np.vstack([a, b]),fmt='%f')

The resulting file looks like this:

1.000000 2.000000 3.000000
2.000000 3.000000 4.000000

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