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.
array2stringis really just for showing arrays to the user, not for writing them out in any particular format.np.savetxtmight be what you want.