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

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

Fixed some bad imports in main na_file modules.

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    """
60    rtitems = []
61    lines = []       
62    if type(object) == type([2,3]):
63        nextitem = object[0]
64        del object[0]
65
66        while len(rtitems) < nitems:   
67            items = nextitem.strip().split()
68            lines.append(items)
69            (rtitems, extras) = (rtitems + items[:nitems], items[nitems:])
70
71    else:
72        lines = []
73        while len(rtitems) < nitems:
74            items = object.readline().strip().split()
75            lines.append(items)
76            (rtitems, extras) = (rtitems + items[:nitems], items[nitems:])
77
78    if len(extras) > 0:
79        raise Exception("Could not split " + `len(lines)` + " lines exactly into required number (" + `nitems` + ") of items: \n" + str(lines))
80
81    if rttype is not str:
82        rtitems = [rttype(x) for x in rtitems]
83
84    if type(object) == type([1,2]):
85        return (rtitems, object)
86    else:
87        return rtitems
88"""
89
90    rtitems=[]
91    lines=[]
92    if type(object) is list:
93        nextitem=object[0]
94        del object[0]
95        while len(rtitems)<nitems:
96            #items=re.split(r"\s+", nextitem.strip())
97            items=nextitem.strip().split()
98            lines.append(items)
99            (rtitems,extras)=(rtitems+items[:nitems],items[nitems:])
100        if len(extras)>0:
101            raise "Could not split %s lines exactly into required number (%s) of items: \n%s" % (len(lines), nitems, lines)
102        if rttype is not str:
103            rtitems = [rttype(x) for x in rtitems]
104        return (rtitems, object)
105    else:
106        lines =[]
107        while len(rtitems)<nitems:
108            #items=re.split(r"\s+", object.readline().strip())
109            items=object.readline().strip().split()
110            lines.append(items)
111            (rtitems,extras)=(rtitems+items[:nitems], items[nitems:])
112        if len(extras)>0:
113            raise "Could not split %s lines exactly into required number (%s) of items: \n%s" % (len(lines), nitems, lines)
114        if rttype is not str:
115            rtitems = [rttype(x) for x in rtitems]
116        return rtitems
117
Note: See TracBrowser for help on using the repository browser.