#ix_grep_v00.py def ix_grep_v00(targetStr,file,splitStr,stripStr,commStr): #by D@CC #on 2023LDec03 #status: works well #parm1: targetStr, string to find in file #parm2 file, fileName eg ix_eDict.txt # file="/home/pi/IX_assets/ix/ix_eDict.txt" #parm3 splitStr, "=" #parm4 stripStr eg '"' #parm5 comment character eg "#" #returns: last result #Purpose: grep-like targetStr in file # ignore comments (begin with "#" selectable) # split on splitStr eg "=" unless null # strip unprintables # if any '"' # strip '"' beg and end '"' # Note: if splitStr="", it works like grep # except only returns the last record found # except optionally skips all comment recs # Note: if targetStr is null, the file is loaded into a list # one record per item in the list # comment records are ignored # D@CC uses this mode # result=ix_grep(parm1,file,"=",'"',"#")#one rec containing 1 value, skip # #print("30 targetStr:"+targetStr) if targetStr=="": # print("creatng list") result=[] #making a list in result f=open(file,"r") for rec in f: rec=rec.strip("\n") #print("read rec:"+rec) if not rec[0:1]==commStr: result.append(rec) #of emd #result.append("record ",cnt) #for end return result else: pass #process this target string #if end #print("48 targetStr:"+targetStr) cnt=0 result="none" for rec in open("ix_eDict.txt"): #result="none" cnt+=1 #print("at 55 rec:"+rec) if not rec[0:1]==commStr: #if cnt==7: if True: if splitStr=="": #print("at 40 splitStr is null") colT=rec.find(targetStr) if colT>-1: #print("at 63, returning whole rec (like greb)") result=rec #if end else: #splitStr is not null #print("at 67 rec:"+rec) rec=rec.strip("\n") #print("at 50 splitStr:"+splitStr) colTarget=rec.find(splitStr) #print("at 72 colTarget:",colTarget) if colTarget>-1: #print("at 75 colTarget:",colTarget) colSplit= rec.find(splitStr) #print("at 77 colSplit:",colSplit) if colSplit>-1: target,strValue=rec.split(splitStr) #print("at 80 target:"+target+":") #print("at 81 targetStr:"+targetStr+":") if target==targetStr : #print("at 83 strValue:"+strValue+":") strValue=strValue.strip() # strips spaces value=strValue.strip(stripStr) # strips '"' #print("at 86 target:"+target+":") #print("at 87 cnt,value:",cnt,value+":") result=value #print("at 89 result:"+result+":") return result #if end #if end #if end #if end #if end #if end #for end #print("at 98 result:"+result) return result #def end ix_grep_v00() #/#ix_grep_v00.py