IronPython: Get Unique values from Data Column
IronPython Script to extract the unique values from a Data Tables' Column.
#-------Gets Unique values from a Column---------#
def fnGetUniqueColVal(TblName,ColName):
#Get Table and
Column References
dtRef=Document.Data.Tables[TblName]
ColCursor=DataValueCursor.CreateFormatted(dtRef.Columns[ColName])
fnUnique = ""
fnUniquearr = {}
for row in dtRef.GetRows(ColCursor):
rowIndex = row.Index
fnUniquearr[ColCursor.CurrentValue]
= 1
for key, value in fnUniquearr.items() :
fnUnique = fnUnique + key + ","
# Return either
a String or the Unique list
return fnUnique[:-1]
#return
fnUniquearr
def fnGetUniqueFiltColVal(TblName,ColName,FiltScheme):
#Get Table and
Column References
dtRef=Document.Data.Tables[TblName]
ColCursor=DataValueCursor.CreateFormatted(dtRef.Columns[ColName])
filtRows =
Document.Data.Filterings[FiltScheme].GetSelection(dtRef).AsIndexSet()
fnUnique = ""
fnUniquearr = {}
for row in dtRef.GetRows(filtRows,ColCursor):
rowIndex = row.Index
fnUniquearr[ColCursor.CurrentValue]
= 1
for key, value in fnUniquearr.items() :
fnUnique = fnUnique + key + ","
# Return either
a String or the Unique list
return fnUnique[:-1]
#return
fnUniquearr
###----------------------------------------------------------------------------###
How to use the Function
#print
fnGetUniqueColVal(TableName,ColumnaName)
print fnGetUniqueColVal("SalesAndMarketing","State")
#print
fnGetUniqueFiltColVal(TableName,ColumnaName,FilteringScheme)
print fnGetUniqueFiltColVal("SalesAndMarketing","State","FilteredRows")
** As mentioned previously, most of the scripts we provide on our website will be developed as functions, which will enable us to both reuse and build up a library as we go along.**
Comments
Post a Comment