source: nappy/trunk/nappy/utils/text_parser.py @ 3403

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/utils/text_parser.py@3403
Revision 3403, 2.5 KB checked in by astephen, 13 years ago (diff)

Fixed a bug due to indentation.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6textParser.py
7=============
8
9A set of functions to parse text file data into lists, strings,
10reals, integers etc.
11
12"""
13
14import re
15import string
16
17pattnNoQuotes = re.compile("^[\"'].*\1$")
18
19def readItemFromLine(line, rttype=str):
20    """
21
22    """
23    rtitem = pattnNoQuotes.sub("", line.strip())
24    if rttype is not str:
25        rtitem = rttype(rtitem)   
26    return rtitem
27
28def readItemsFromLine(line, nitems=None, rttype=str):
29    """
30
31    """
32    rtitems = re.split(r"\s+", line.strip())
33    if nitems and len(rtitems) != nitems:
34        raise "Incorrect number of items (%s) found in line: \n'%s'" % (nitems, line)
35    if rttype is not str:
36        rtitems = [rttype(x) for x in rtitems]
37    return rtitems
38
39def readItemsFromLines(lines, nitems, rttype=str):
40    """
41
42    """
43    rtitems = []
44    for line in lines:
45        rtitems = rtitems + [readItemFromLine(line, rttype)]
46    if rttype is not str:
47        rtitems = [rttype(x) for x in rtitems]
48    return rtitems
49
50def readItemsFromUnknownLines(object, nitems, rttype=str):
51    """
52    Reads from an unknown number of lines until n items have been collected.
53    The 'object' argument can be a filehandle (i.e. obj=open('name.ext', 'r'))
54    or a string wrapped in a StringIO object (i.e. obj=StringIO.StringIO('abc')).
55    The 'object' argument can also be a list, in which case the partially used/read object is
56    also returned.
57    """
58
59    rtitems = []
60    lines = []       
61    if type(object) == type([2,3]): 
62
63        while len(rtitems) < nitems:   
64            nextitem = object[0]
65            object = object[1:]
66            items = nextitem.strip().split()
67            lines.append(items)
68            (rtitems, extras) = (rtitems + items[:nitems], items[nitems:])
69
70    else:
71        while len(rtitems) < nitems:
72            items = object.readline().strip().split()
73            lines.append(items)
74            (rtitems, extras) = (rtitems + items[:nitems], items[nitems:])
75
76    if len(extras) > 0:
77        raise Exception("Could not split " + `len(lines)` + " lines exactly into required number (" + `nitems` + ") of items: \n" + str(lines))
78
79    if rttype is not str:
80        rtitems = [rttype(x) for x in rtitems]
81
82    if type(object) == type([1,2]):
83        return (rtitems, object)
84    else:
85        return rtitems
Note: See TracBrowser for help on using the repository browser.