Numpy.genfromtxt: Ambiguous Delimiters?

I am trying to write a generic script, part of which imports files separated by commas or separated by spaces. I would like the script to recognize any type. Is there a way to specify something like

arrayobj = np.genfromtxt(file.txt, delimiter=(',' OR '\t'), names=None, dtype=None) 

I tried using the regex ( ',|\t' ), but that doesn't work either.

+7
python numpy delimiter genfromtxt
source share
1 answer

As already mentioned, I do not believe that there is a way to do this using np.genfromtxt ; however you can always use python pandas.

 example.txt: 1,2,3 #Header 1,2,3 4,5'tab'6 7'tab'8'tab'9 

Using pandas read_csv :

 print pd.read_csv('example.csv',sep='\t|,').values [[1 2 3] [4 5 6] [7 8 9]] 
+1
source share

All Articles