Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.genfromtxt: Ambiguous delimiters?

I'm trying to write a generic script, part of which imports files that are either comma-separated or white-space-separated. I'd like the script to recognize either type. Is there a way to specify something like

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

I tried using a regular expression (',|\t') but that doesn't work either.

like image 897
PikalaxALT Avatar asked Jul 18 '13 16:07

PikalaxALT


1 Answers

As mentioned I do not believe there is a way to do this with 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]]
like image 194
Daniel Avatar answered Oct 17 '22 19:10

Daniel