I would like to write lists as columns in a txt file. I tried to do this like so:
lst_h_names=['name_1','namenamename','nam3','4']
lst_h_1_cont=[1,2,3000,4]
lst_h_2_cont=[1,2000,3,4]
lst_scaling_factor=[10,2,3,4]
array_h_names=np.array(lst_h_names)
array_h_1_cont=np.array(lst_h_1_cont)
array_h_2_cont=np.array(lst_h_2_cont)
array_scaling_factor=np.array(lst_scaling_factor)
norm_factors=np.array([array_h_names,array_h_1_cont,array_h_2_cont,array_scaling_factor])
norm_factors=norm_factors.T
print(norm_factors)
norm_factors_path='all_factors_np.txt'
with open(norm_factors_path,'w') as fil:
np.savetxt(fil,norm_factors,fmt=['%s','%i','%i','%i'])
This raises the following error:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
1433 try:
-> 1434 v = format % tuple(row) + newline
1435 except TypeError:
TypeError: %i format: a number is required, not numpy.str_
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-51-5b3aeaae6c1a> in <module>
20 norm_factors_path='all_factors_np.txt'
21 with open(norm_factors_path,'w') as fil:
---> 22 np.savetxt(fil,norm_factors,fmt=['%s','%i','%i','%i'])
<__array_function__ internals> in savetxt(*args, **kwargs)
/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
1436 raise TypeError("Mismatch between array dtype ('%s') and "
1437 "format specifier ('%s')"
-> 1438 % (str(X.dtype), format))
1439 fh.write(v)
1440
TypeError: Mismatch between array dtype ('<U21') and format specifier ('%s %i %i %i')
I understand why this doesn't work, but I don't quite know how to do it differently
You are trying to write an array with string dtype:
In [11]: norm_factors
Out[11]:
array([['name_1', '1', '1', '10'],
['namenamename', '2', '2000', '2'],
['nam3', '3000', '3', '3'],
['4', '4', '4', '4']], dtype='<U21')
As others point out, only '%s' can format that.
savetxt
iterates on rows of the array, and formats and writes it
fmt%tuple(row)
As the error notes, it has converted your fmt
into ''%s %i %i %i'. So it's try to:
'%s %i %i %i'%tuple(norm_factors[0])
An alternative to the '%s %s %s %s' is to make an object
dtype array:
In [26]: norm_factors=np.array([array_h_names,array_h_1_cont,array_h_2_cont,array_scaling_factor], dtype=object).T
In [27]: norm_factors
Out[27]:
array([['name_1', 1, 1, 10],
['namenamename', 2, 2000, 2],
['nam3', 3000, 3, 3],
['4', 4, 4, 4]], dtype=object)
In [28]: fmt = '%s %i %i %i'
In [29]: fmt%tuple(norm_factors[0])
Out[29]: 'name_1 1 1 10'
You could also make a structured array, but the object dtype is simpler.
And the zip
lists approach is just as good.
You could use pandas which is convenient:
import pandas as pd
pd.DataFrame(norm_factors).to_csv('yourfile.txt',header=None, index=None)
Try this to see if it works:
>>> np.savetxt(fil,norm_factors,fmt=['%s','%s','%s','%s'])
%s
can handle both string types and integer types in formatting. But to get more precise formatting, you can refer to the link I have below.
The problem that the error message is telling you is you're trying to represent a string type as a numeric type:
TypeError: Mismatch between array dtype ('<U21') and format specifier
('%s %i %i %i')
Here's a reference on string formatting:
https://docs.python.org/2/library/string.html#format-specification-mini-language
The other answers give some good advice on other aspects of your code.
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.