7

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.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
PikalaxALT
  • 327
  • 3
  • 12
  • 4
    I dont believe you can use np.genfromtxt for this. The delimiter argument is passed directly to the python standard library `str.split(delimiter)`. – Daniel Jul 18 '13 at 18:42

1 Answers1

1

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]]
Daniel
  • 19,179
  • 7
  • 60
  • 74