python - Why doesn't my array space correctly when I use np.savetxt -


why doesn't text file space output correctly?

import numpy np my_list =['str1', 'str2', 'str3'] my_list2=[1,2,3] print(my_list) 

['str1', 'str2', 'str3'] printed.

my_array=np.array(my_list) my_array2=np.array(my_list2) combined=np.column_stack([my_array,my_array2]) np.savetxt('test_file_name.txt', combined, fmt="%s") print(combined) 

the print gives:

[['str1' '1']  ['str2' '2']  ['str3' '3']] 

the file says str1 1str2 2str3 3. want say:

str1 1  str2 2  str3 3 

running windows 8.1, using notepad view file.

short answer: notepad worst. use notepad++.

appending question: there way have columns line nicer? of data has few characters, while others have fifteen or so.

if print array not see commas normal behaviour numpy, if use repr when printing i.e print repr(np.array([1, 2, 3])) or run python shell or ipython without printing show repr see in scipy docs.

there no difference @ whether see repr output or formatted output, array shape not change has nothing issue:

in [13]: out[13]:  array(['1', '2', '3'],  # repr       dtype='|s1')  in [14]: b out[14]:  array(['2', '3', '4'], # repr       dtype='|s1')  in [15]: print(a) # str ['1' '2' '3']  in [16]: print(b) # str ['2' '3' '4']  in [17]: print(np.column_stack((a,b))) [['1' '2']  ['2' '3']  ['3' '4']] 

if having issues column_stack, not because of how arrays when print them.

your output want in file:

str1 1 str2 2 str3 3 

the way see getting str1 1str2 2str3 3 in file if used newline="" data end on single line.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -