问题
I have been attempting to extract data from excel files, convert it into an array, then write it into some other currently undefined file type (so a .txt file is the current placeholder file type). I'm sure the code is rather ugly, but it works:
import os
import pandas as pd
import glob
import numpy as np
def xlxtract():
for filename in glob.glob('*.xlsx'):
ExcelFile = filename[:-5]
RosewoodData = pd.read_excel(ExcelFile + '.xlsx')
DataMatrix = np.array(RosewoodData)
DataMatrixString = np.array2string(DataMatrix, precision=4, separator=' ')
NewFile = open(ExcelFile + 'MATRIX.txt', 'w')
NewFile.write(' ' + DataMatrixString[1:-1])
NewFile.close()
print('Your file has been printed to ' + ExcelFile + '.txt')
Anyway, the problem I am running into is that, while it does print to the .txt file, the brackets are not removed. The output looks as such (with random numbers generated as a test):
[ 20 6]
[ 76 2]
[ 93 97]
[ 29 75]
[ 75 69]
[ 77 81]
[ 19 51]
[ 70 100]
[ 94 68]
I would like to remove the brackets, but there don't seem to be any one line methods to do this. Any help would be appreciated.
回答1:
array2string
formats the array for display, same as if you were to do a print
:
In [32]: x = np.arange(12).reshape(4,3)
In [33]: x
Out[33]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In [34]: np.array2string(x)
Out[34]: '[[ 0 1 2]\n [ 3 4 5]\n [ 6 7 8]\n [ 9 10 11]]'
Note that the print string of a list also includes the brackets (and commas):
In [35]: str(x.tolist())
Out[35]: '[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]'
Another complication with array2string
is that it uses the ellipsis abbreviation for long arrays (though that can be changed with a parameter).
np.savetxt
is a relatively simple write of a 2d array to a file, and can be emulated with explicit formatting.
In [37]: np.savetxt('test.txt', x, fmt='%d', delimiter=',')
In [38]: cat test.txt # ipython system command to display a file
0,1,2
3,4,5
6,7,8
9,10,11
In [39]: for row in x:
...: print('%d,%d,%d'%tuple(row))
...:
0,1,2
3,4,5
6,7,8
9,10,11
or as one string
In [42]: astr = '\n'.join(['%3d %3d %3d'%tuple(row) for row in x])
In [43]: astr
Out[43]: ' 0 1 2\n 3 4 5\n 6 7 8\n 9 10 11'
来源:https://stackoverflow.com/questions/52509537/np-array2string-not-removing-brackets-around-an-array