#!/usr/bin/python -Wall # ================================================================ # John Kerl # kerl at math dot arizona dot edu # 2009-10-08 # Please see http://math.arizona.edu/~kerl/doc/python-talk.pdf for # more information. # ================================================================ import sys import re # ---------------------------------------------------------------- def text_columns_from_file(file_name): """Given an input file containing tabular data, returns a list of columns. Pound signs may be used as comments; blank lines are skipped. For example, if the input file is 1 2 3 4 5 6 # Comment here 7 8 9 a b c then this routine returns [['1', '4', '7', 'a'], ['2', '5', '8', 'b'], ['3', '6', '9', 'c']]. """ if (file_name == "-"): file_handle = sys.stdin else: try: file_handle = open(file_name, 'r') except: print >> sys.stderr, "Couldn't open \"" + file_name + "\" for read." sys.exit(1) columns = [] num_fields = -1 line_number = 0 while 1: line = file_handle.readline() if (line == ""): break line_number += 1 # Strip trailing carriage return, if any. if line[-1] == '\n': line = line[0:-1] # Strip comments. line = re.sub(r"#.*", r"", line) # Strip leading and trailing whitespace. line = re.sub(r"^\s+", r"", line) line = re.sub(r"\s+$", r"", line) # Skip blank lines. if re.match(r"^$", line): continue # Split on whitespace. fields = line.split() curr_num_fields = len(fields) if (num_fields == -1): # First data line of file. num_fields = curr_num_fields for j in range(0, num_fields): columns.append([]) elif curr_num_fields != num_fields: print >> sys.stderr, "Ragged input in file \"%s\" at line %d." % \ (file_name, line_number) sys.exit(1) for j in range(0, num_fields): columns[j].append(fields[j]) if (file_name != "-"): file_handle.close() return columns # ---------------------------------------------------------------- def float_columns_from_file(file_name): """Given an input file containing float tabular data, returns a list of columns. Pound signs may be used as comments; blank lines are skipped. For example, if the input file is 1 2 3 4 5 6 # Comment here 7 8 9 then this routine returns [[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]] """ columns = text_columns_from_file(file_name) num_rows = len(columns) num_cols = len(columns[0]) for i in range(0, num_rows): for j in range(0, num_cols): columns[i][j] = float(columns[i][j]) return columns # ---------------------------------------------------------------- def float_columns_to_file(columns, file_name, format="%11.7f"): """Given a list of columns, write the data to a table file. For example, if the input is [[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]] then the output file contains 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 """ if (file_name == "-"): file_handle = sys.stdout else: try: file_handle = open(file_name, 'w') except: print >> sys.stderr, \ "Couldn't open \"" + file_name + "\" for write." sys.exit(1) num_columns = len(columns) num_rows = len(columns[0]) for i in range(0, num_rows): for j in range(0, num_columns): print >> file_handle, format % columns[j][i], print >> file_handle, "" if (file_name != "-"): file_handle.close()