Entry
ASCII delimited files
Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 290, Roger Irwin
"""
Packages: text.delimited_files
"""
"""
Thanks to everybody who sent me suggestions for reading delimited ASCII files
last week. As there was no standard funtion for doing what I wanted I wrote my
own, but as this could be very useful to a lot of other people (if nothing alse
for reading in data from spreadsheets), I thought others might like a copy of
this simple function:
(Note, this is more orientated towards the requirements of general data tables
rather than mathematical data sets).
"""
pdict={}
def parsedata(ftr,delim=';',offset=0,strip=1):
"Read a delimited ASCII file into a dictionary"
"After opening the file and reading in any header lines,"
"this function can be used with ftr= to the open file object"
"The delim parameter is obvious whilst the offset points to"
"the field to be used as the key."
"If strip is true, then if a field starts with a quotation"
"the first and last character will ripped off the field."
"A line with no delimiter will be regarded as a comment"
"Stops reading as soon as a control character is encountered"
"Returns number of records read"
counter=0
while 1:
foo=ftr.readline()
if foo=='':
break
start=0
buf=[]
parsetab=[]
for each in range(len(foo)):
if foo[each]==delim:
if (foo[start]=='"')|(foo[start]=="'"):
parsetab.append(foo[start+1:each-1])
else:
parsetab.append(foo[start:each])
start=each+1
elif foo[each]<' ':
if start:
if (foo[start]=='"')|(foo[start]=="'"):
parsetab.append(foo[start+1:each-1])
else:
parsetab.append(foo[start:each])
break
if start:
pdict[parsetab[offset]]=parsetab
counter=counter+1
return counter