Numpy Loadtxt Skip First Row
I have a small issue when I'm trying to import data from CSV files with numpy's loadtxt function. Here's a sample of the type of data files I have. Call it 'datafile1.csv': # Comm
Solution 1:
Skip comment line manually using generator expression:
import numpy as np
withopen('datafile1.csv') as f:
lines = (line for line in f ifnot line.startswith('#'))
FH = np.loadtxt(lines, delimiter=',', skiprows=1)
Solution 2:
Create your own custom filter function, such as:
defskipper(fname):
withopen(fname) as fin:
no_comments = (line for line in fin ifnot line.lstrip().startswith('#'))
next(no_comments, None) # skip headerfor row in no_comments:
yield row
a = np.loadtxt(skipper('your_file'), delimiter=',')
Solution 3:
defskipper(fname, header=False):
withopen(fname) as fin:
no_comments = (line for line in fin ifnot line.lstrip().startswith('#'))
if header:
next(no_comments, None) # skip headerfor row in no_comments:
yield row
a = np.loadtxt(skipper('your_file'), delimiter=',')
This is just a little modification of @Jon Clements's answer by adding an optional parameter "header", given that in some cases, the csv file has comment lines (starts with #) but doesn't have the header row.
Post a Comment for "Numpy Loadtxt Skip First Row"