From 7d5bf9e7f863d07b80582a0de93607785afaa1d2 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Wed, 27 Aug 2014 22:22:59 -0400 Subject: [PATCH] * Added simple text table reader. --- text_tools.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/text_tools.py b/text_tools.py index b216af6..204f7a7 100644 --- a/text_tools.py +++ b/text_tools.py @@ -16,6 +16,25 @@ Frequently used text tools. import numpy from wpylib.sugar import ifelse +def read_text_table(F, maps={}, sep=None, comment_char="#"): + """Reads in a 2-D table from a text stream. + Returns a list of lists containing the table content, in each cell by + default as a string, unless a mapping function is provided (for simple + data conversion only).""" + rows = [] + for L in F: + if comment_char != None: + L = L.split(comment_char,1)[0] + flds = L.split(sep) + if len(flds) == 0: + continue + if maps: + for i in xrange(len(flds)): + if i in maps: + flds[i] = maps[i](flds[i]) + rows.append(flds) + return rows + def make_matrix(Str, debug=None): """Simple tool to convert a string like '''1 2 3