When using np.genfromtxt(), can we use a different delimiter?


#1

Question

When using genfromtxt() in Numpy, can we use a different delimiter value?

Answer

Yes, you can!

When using the genfromtxt() function, the value for the delimiter argument can be any sequence of characters to split the text on each line of the file. Each string of text will be split on the specified delimiter value.

Usually, the value is a single character, such as a comma (,) or semicolon (;). But, you can also use strings as delimiters. Regardless of what you use, it will split each line of text on that sequence of characters. It’s similar to the .split() method in Python.

Example

# Say a CSV file 'example.csv' contains the following data

'''
10blah20blah30
40blah50blah60
'''

np.genfromtxt('example.csv', delimiter='blah') 
# will result in
array([10, 20, 30],
      [40, 50, 60])