From 686126995968064d2ca20b4319ffa71cdb9be736 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 6 May 2008 13:37:40 +0200 Subject: [PATCH] getListText inverse of getTextList --- oxutils/text.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/oxutils/text.py b/oxutils/text.py index ed83290..cc028be 100644 --- a/oxutils/text.py +++ b/oxutils/text.py @@ -139,6 +139,30 @@ def getTextList(list_, last_word='or'): if len(list_) == 1: return list_[0] return '%s %s %s' % (', '.join([str(i) for i in list_][:-1]), last_word, list_[-1]) +def getListText(text, last_word='or'): + """ + >>> getListText('a, b, c or d') + ['a', 'b', 'c', 'd'] + >>> getListText('a, b and c', 'and') + ['a', 'b', 'c'] + >>> getListText('a and b', 'and') + ['a', 'b'] + >>> getListText('a') + ['a'] + >>> getListText('') + [] + """ + list_ = [] + if text: + list_ = text.split(', ') + if list_: + i=len(list_)-1 + last = list_[i].split(last_word) + if len(last) == 2: + list_[i] = last[0].strip() + list_.append(last[1].strip()) + return list_ + def normalizeNewlines(text): return re.sub(r'\r\n|\r|\n', '\n', text)