In [ ]:
from IPython.display import display_pretty
import numpy as np
from io import StringIO, BytesIO

I/O with numpy

importing data with genfromtext

  • spliting lines into columns
  • skiping lines and choose columns
  • choosing the data type
  • setting the names
  • tweaking the conversion
  • Using missing and filling values

In [ ]:
data = '''
c1, c2, c3
1, 2, 3
// here is the comment line
4, 5, 6
7,8,9
-1, -1, -1
end, end, end
'''.encode()
a = np.genfromtxt(BytesIO(data), delimiter=',', comments='//')
b = np.genfromtxt(BytesIO(data), delimiter=',', dtype='|S5', autostrip=True, comments='//')
c = np.genfromtxt(BytesIO(data), delimiter=',', comments='//', skip_header=2, skip_footer=1)
d = np.genfromtxt(
    BytesIO(data),
    delimiter=',',
    comments='//',
    skip_header=1, skip_footer=1,
    names=True,
    usecols=('c1', 'c3'))
e = np.genfromtxt(
    BytesIO(data), delimiter=',', autostrip=True, comments='//',
    skip_header=2, skip_footer=1,
    converters={0: lambda x: float(x) ** 2})
display_pretty(a, b, c, d, e)