source: nappy/trunk/textParser.py @ 349

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/textParser.py@349
Revision 349, 2.9 KB checked in by selatham, 15 years ago (diff)

Inserted license information.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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    rtitem=pattnNoQuotes.sub("", line.strip())
21    if rttype==int:
22        rtitem=int(rtitem)
23    elif rttype==float:
24        rtitem=float(rtitem)       
25    return rtitem
26
27def readItemsFromLine(line, nitems=None, rttype=str):
28    rtitems=re.split(r"\s+", line.strip())
29    if nitems and len(rtitems)!=nitems:
30        raise "Incorrect number of items (%s) found in line: \n'%s'" % (nitems, line)
31    if rttype==int:
32        rtitems=map(lambda x: int(x), rtitems)
33    elif rttype==float:
34        rtitems=map(lambda x: float(x), rtitems)       
35    return rtitems
36
37def readItemsFromLines(lines, nitems, rttype=str):
38    rtitems=[]
39    for line in lines:
40        rtitems=rtitems+[readItemFromLine(line, rttype)]
41    if rttype==int:
42        rtitems=map(lambda x: int(x), rtitems)
43    elif rttype==float:
44        rtitems=map(lambda x: float(x), rtitems)           
45    return rtitems
46
47def readItemsFromUnknownLines(object, nitems, rttype=str):
48    """Reads from an unknown number of lines until n items have been collected.
49    The 'object' argument can be a filehandle (i.e. obj=open('name.ext', 'r'))
50    or a string wrapped in a StringIO object (i.e. obj=StringIO.StringIO('abc')).
51    The 'object' argument can also be a list, in which case the unused lines are
52    also returned.
53    """
54    rtitems=[]
55    lines=[]   
56    if type(object)==list:
57        while len(rtitems)<nitems:
58            nextitem=object[0]
59            object=object[1:]
60            items=re.split(r"\s+", nextitem.strip())
61            lines.append(items)       
62            (rtitems, extras)=(rtitems+items[:nitems], items[nitems:])
63        if len(extras)>0:
64            raise "Could not split %s lines exactly into required number (%s) of items: \n%s" % (len(lines), nitems, lines)
65        if rttype==int:
66            rtitems=map(lambda x: int(x), rtitems)
67        elif rttype==float:
68            rtitems=map(lambda x: float(x), rtitems)
69        return (rtitems, object)
70    else:
71        while len(rtitems)<nitems:
72            items=re.split(r"\s+", object.readline().strip())
73            lines.append(items)       
74            (rtitems, extras)=(rtitems+items[:nitems], items[nitems:])
75        if len(extras)>0:
76            raise "Could not split %s lines exactly into required number (%s) of items: \n%s" % (len(lines), nitems, lines)
77        if rttype==int:
78            rtitems=map(lambda x: int(x), rtitems)
79        elif rttype==float:
80            rtitems=map(lambda x: float(x), rtitems)     
81        return rtitems
82
83
84
85
86   
87   
88
Note: See TracBrowser for help on using the repository browser.