I wish to check if in a text file of points (x,y,z, etc) where is an header (True) or not (False). I wish to know if there is a built-in function in Python or a better method respect my own function.
def check_header(filename, parse):
with open(filename) as f:
first = f.readline()
line = first.rstrip().split(parse)
try:
float(line[0])
return False
except ValueError:
return True
i wrote this function example
a b c d
449628.46 6244026.59 0.47 1
449628.55 6244033.12 0.30 2
449628.75 6244046.31 0.37 3
449628.81 6244049.63 0.44 1
449628.81 6244049.88 0.39 5
449628.81 6244050.66 0.30 1
449628.96 6244060.67 0.38 2
449629.18 6244075.61 0.39 2
449629.24 6244078.72 0.47 4
449629.24 6244078.96 0.41 8
449629.23 6244079.19 0.34 4
check_header(filename, " ")
True
449628.46 6244026.59 0.47 1
449628.55 6244033.12 0.30 2
449628.75 6244046.31 0.37 3
449628.81 6244049.63 0.44 1
449628.81 6244049.88 0.39 5
449628.81 6244050.66 0.30 1
449628.96 6244060.67 0.38 2
449629.18 6244075.61 0.39 2
449629.24 6244078.72 0.47 4
449629.24 6244078.96 0.41 8
449629.23 6244079.19 0.34 4
check_header(filename, " ")
False
csv][1] module in the stdlib (you just need to passdelimiter=' '), which may be a little simpler and a lot more robust than whatever custom code you're doing. And you might want to consider switching to the commas as separators instead of spaces (which would, e.g., make it trivial to add column names with spaces in them, without having to handle quoting).