#create csv_query.py in /Extensions and paste below code into it
def _parseFile(file):
""" responsible for parsing the file and returning the datastructure """
parsed={}
columns=[] #in order of appearance
f=open(file, 'r')
lines=f.readlines()
for column in lines[0].split(','):
column=column.strip()
columns.append(column)
parsed[column]=()
for line in lines[1:]:
data=line.strip().split(',')
for x in range(0, len(columns)):
column_name=columns[x]
values=parsed[column_name]
parsed[column_name]=values+(data[x], )
f.close()
return parsed
def _queryCSVMapping(map, field, value):
""" return the position (of the sequence of dat) inside the value of the mapping """
values=map[field]
results=() # line numbers where found
for x in range(0, len(values)):
if value==values[x]:
results=results+(x,)
return results
def _getDataFromLineNumbers(file, line_nums):
data=() #sequence of text found
f=open(file, 'r')
contents=f.readlines()
for line_num in line_nums:
data=data+(contents[line_num+1],)
f.close()
return data
def csv_query(file, field, value):
csv_mapping=_parseFile(file)
line_number=_queryCSVMapping(csv_mapping, field, value)
lines=_getDataFromLineNumbers(file, line_number)
return lines #this is sequence of raw lines found
#in ZOPE create a External Method
#id: queryCSVFile
#module: csv_query
#function: csv_query
#create a datafile, lets say data.csv in /Extensions
John Smith,111-1111,123456789
John Smith,222-2222,123456789
Betty Lee,333-3333,234567890
Joe Anderson,444-4444,345678901
#now in your Script(Python)
f='e:\\zope25b1\\extensions\\data.csv' #path to file
data=context.queryCSVFile(f, 'phone', '222-2222')
return data
#and you should be returned
John Smith,222-2222,123456789
#you could in your DTMLMethod call it by
#and you should get
John Smith,111-1111,123456789
John Smith,222-2222,123456789