1

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.

7
  • DataMatrixString[1:-1] should remove the brackets.. Commented Sep 26, 2018 at 4:17
  • array2string is really just for showing arrays to the user, not for writing them out in any particular format. np.savetxt might be what you want. Commented Sep 26, 2018 at 8:32
  • @bakka, That is what it seemed like from what I've been reading, but the text files still show brackets. It removed the initial and ending brackets of the array but does not remove the brackets of the individual rows of said array. Commented Sep 26, 2018 at 14:11
  • @Eric, I looked into np.savetxt quickly and it seems like this is solely used to save text to a .txt file. While I could work with this, and just add another portion of code to convert the .txt file to the file type I end up needing, it would be preferred to have a function that is dynamic about which file types it can save data as. Commented Sep 26, 2018 at 14:13
  • What other kind(s) of file types might you be interested in? Commented Sep 27, 2018 at 2:33

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.