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.
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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With