I have searched various questions on here, but am unable to find an adequate solution to how to save my array to be readable in a text file. I have a numpy array with shape (13,5) that contains strings . When I use np.savetxt, it all prints on one line.
More array info: type: class 'numpy.ndarray', type of entries: class 'numpy.str_'.
Here is the line I use to print my array:
np.savetxt('file_name.txt', array_name, fmt="%s")
Why does it print on one line? How can I make it print in an easy to read fashion (not all on one line)?
array_name=np.array(
[['Champion' 'Wins' 'Plays' 'Win %' 'Popularity'],
['Ahri' '17' '25' '68.0' '1.25'],
['Akali' '4' '7' '57.14' '0.35'],
['Alistar' '28' '56' '50.0' '2.8'],
['Amumu' '3' '4' '75.0' '0.2'],
['Anivia' '5' '6' '83.33' '0.3'],
['Annie' '1' '9' '11.11' '0.45'],
['Ashe' '7' '11' '63.64' '0.55'],
['Azir' '16' '28' '57.14' '1.4'],
['Bard' '19' '34' '55.88' '1.7'],
['Blitzcrank' '9' '16' '56.25' '0.8'],
['Brand' '0' '1' '0.0' '0.05'],
['Braum' '5' '16' '31.25' '0.8']])
OK, now that you've provided a sample array, it's easy to see why you get the result you describe: you don't have commas separating what are supposed to be distinct strings in each row. Python concatenates adjacent string literals:
>>> 'a' 'b' 'c'
'abc'
Thus your array has 1 column, not 5:
>>> array_name.shape
(13, 1)
>>> array_name[0, 0]
'ChampionWinsPlaysWin %Popularity'
>>> array_name[0, 1]
IndexError Traceback...
...
IndexError: index 1 is out of bounds for axis 1 with size 1
Yes, more info needed -- I can't reproduce what you describe. After running this:
import numpy as np
ra = np.array([['Here', 'is', 'my'],
['array', 'of', 'strings']])
np.savetxt('temp.txt', ra, fmt="%s")
the file temp.txt
contains:
Here is my
array of strings
One line per row, each ending with '\\n'
, with items in each row separated by spaces.
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.