def getStringnotabsspaces(stringline): def findnonemptyrts(stringset, itera, map2): returnit = 0 index1, index2 = map2[itera] if stringset[index1][index2] == '': returnit = findnonemptyrts(stringset, itera-1,map2) else: return itera return returnit def findnonemptylts(stringset, itera, map2): returnit = 0 index1, index2 = map2[itera] if stringset[index1][index2] == '': returnit = findnonemptylts(stringset, itera+1,map2) else: return itera return returnit def map2dlist(listset): count = 0 mapset = {} setindex = 0 for listr in listset: listindex = 0 for strin in listr: mapset[count] = (setindex, listindex) listindex += 1 count += 1 setindex += 1 return mapset def getcopylist(stringset, itera, iterb, map2): index1l, index2l = map2[itera] index1r, index2r = map2[iterb] allset = [] if not index1l == index1r: print('stringset:') print(stringset) retset = [stringset[index1l][index2l:len(stringset[index1l])]] print(retset) if not index1l+1 == index1r: retsetb = stringset[index1l+1:index1r] retsetc = [stringset[index1r][0:index2r+1]] allset = retset+retsetb+retsetc else: retsetb = [stringset[index1r][0:index2r+1]] allset = retset+retsetb else: allset = stringset[index1l][index2l:index2r+1] return allset def buildnfrmsetlr(stringset, adds, addt): namestring = '' ## for i in range(itera,iterb+1): ## namestring += add + stringset[i] count = 0 strngsetcount = 0 for set1 in stringset: setcount = 0 print(set1) for strin in set1: if not count == 0: if setcount == 0: namestring += adds + strin ## print(i) ## print(stringset[i]) else: namestring += addt + strin else: namestring += strin count += 1 setcount += 1 strngsetcount += 1 return namestring def getsplitsets(stringline): splitsets = [] spaceset = stringline.split(' ') count = 0 for word in spaceset: tabset = word.split('\t') splitsets.append(tabset) count += 1 print(splitsets) return splitsets splitsets = getsplitsets(stringline) map2 = map2dlist(splitsets) iterb = findnonemptyrts(splitsets, len(map2)-1, map2) itera = findnonemptylts(splitsets, 0, map2) trunclist = getcopylist(splitsets, itera, iterb, map2) return buildnfrmsetlr(trunclist, ' ', '\t') def getStringnotabsspaces2(stringline): def forward(stringline, itera): retiter = 0 if stringline[itera] == ' ' or stringline[itera] == '\t': retiter = forward(stringline, itera+1) else: if stringline[itera] == '': retiter = forward(stringline,itera+1) else: return itera return retiter def rev(stringline, itera): retiter = 0 if stringline[itera] == ' ' or stringline[itera] == '\t': retiter = rev(stringline, itera-1) else: if stringline[itera] == '': retiter = rev(stringline,itera-1) else: return itera return retiter iter1 = forward(stringline,0) iter2 = rev(stringline, len(stringline)-1) return stringline[iter1:iter2+1]Okay so at present I had the problem of wanting to obtain the body of a string line, that is, the execution set of a string but not wanting tab and space delimited portions of the string outside the body of a given string text. The reason being here is that (as in a previous post on this series), I'd like to be able to have the body text properly tab/space delimited say in python when porting code from another language. The problem is that tab/space delimitation of the original code may not be set as desired, so when setting scope indentation level, that I'd likely append to a given text line, I'd like to have just a given body portion of the execution string line but nothing else. Here's the code. This being the more robust method that I could think of removes all tab and space delimitation s between text in a string line (this can be done using a series of space splitting and tab splitting on the text, but doing so removes all tabs and spaces in a given string line body, so the work around is to preserve the split sets in series (this is a two dimensional array) which indicates where splits on tab and spaces occurred. Unfortunately collapsing the array in the simplest of cases, leaves mapping information between space and tab splits lost, so instead of trying to collapse the array, I created a simple one dimensional count indexing map between all elements of the two dimensional array. This particular map is useful since I can simply iterate on one dimension for a recursion call for searching the non empty elements to the first non empty set element in either direction on the text split sets. Once having this I use python's customary build call having the count index (one dimension) on either endpoint which maps to the two dimensional set telling me what indexes to record to and from when building the string body text back up and re adding tab and space delimitation s where they originally existed in the body text. The alternate shorter hand method it seems would be iterating the string character by character and indexing for the first non empty set character that weren't a tab or space character in either direction. Updated this with two different functions, made corrections to code here. Obviously the second function of the same type, uses a different approach here, namely, iterating through the string array until finding the first non empty character that weren't ' ' or '\t', as opposed to parsing string ordered and split sets of a given string line and then recompiling such string line sets back into a given string omitting spaces and tabs either previous to the body string text or after.
Monday, October 6, 2014
Removing tabs and spaces from the endpoints of a string line in python without removing tab and spacing in the body of a string
Subscribe to:
Post Comments (Atom)
Oblivion
Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...
-
For starters, as I've seen (pardon my ignorance if I am incorrect), it seems a common way to use path based animated motion for objects...
-
Okay so if you read into python scripting examples of moving vertices around in Blender, it seems whether using a bpy.context or bpy.data c...
-
Mesh face and vertex count much to high for game mesh and texture here (? possibly) but someday in the future may be found in textures and ...
No comments:
Post a Comment