Python: reading two columns of numerical data with an arbitrary number of header & blank lines

Here is a method.

The input data may be either space, tab, or comma delimited.

##########################################################################

import os
import re

from numpy import array,zeros,concatenate,linspace,std
from sys import stdin

import matplotlib.pyplot as plt

########################################################################

def start():
print(” “)
print(“Entpath =stdin.readline()
file_path = input_file_path.rstrip(‘\n’)
if os.path.exists(file_path):
print “This file exists”
print ” ”
infile = open(file_path,”rb”)
lines = infile.readlines()
infile.close()
if not os.path.exists(file_path):
print “This file doesn’t exist”
start()
return lines

########################################################################

def lines_two_columns():
#
# http://flockhart.virtualave.net/RBIF0100/regexp.html
#
lines=start()
a = []
b = []
num=0
for line in lines:
#
if re.search(r”(\d+)”, line): # matches a digit
iflag=0
else:
iflag=1 # did not find digit
#
if re.search(r”#”, line):
iflag=1
#
if iflag==0:
line=line.lower()
if re.search(r”([a-d])([f-z])”, line): # ignore header lines
iflag=1
else:
line = line.replace(“,”,” “)
col1,col2=line.split()
a.append(float(col1))
b.append(float(col2))
num=num+1
return a,b,num

##########################################################################

a,b,num =lines_two_columns()

* * *
Tom Irvine

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s