Call a sub-function in a function to deal with a same file in Python? -


i still fight python copy , replace lines, question here. basically, want statistics number of pattern in section, , renew in line. think have found problem in question: call sub-function interate same file in main function, , interation messing @ time. pretty new programming, don't know how copy-statistics-replace-copy thing in way. suggestions or hints welcome.

here part of code got now:

# sum number of keyframes def sumkeys (sceneobj, objname):     sceneobj.seek(0)     block = []     keys = ""     line in sceneobj:         if line.find("objectalias " + objname + "\n") != -1:             line in sceneobj:                 if line.find("beginkeyframe") != -1:                     line in sceneobj:                         if line.find("default") != -1:                             block.append(line.rstrip())                             keys = len(block)                         elif line.find("endkeyframe") != -1:                             break                     break             break     return (keys)  # renew number of keyframes def renewkeys (sceneobj, objname):     sceneobj.seek(0)     newscene = ""     item = []     line in sceneobj:         newscene += line         obj in objname:             if line.find("objectalias " + obj + "\n") != -1:                 line in sceneobj:                     if line.find("endkeyframe") != -1:                         newscene += line                         break                     if line.find("beginkeyframe") != -1:                         item = line.split()                         newscene += item[0] + " " + str(sumkey(sceneobj, obj)) + " " + item[-1] + "\n"                         continue                     else:                         newscene += line     return (newscene) 

original:

lines beginobjects lines objectalias xxx lines beginkeyframe 34 12    ----> 34 want replace lines endobject beginanotherobjects ... 

goal:

lines beginobjects lines objectalias xxx lines beginkeyframe int 12  ---->int comes sumkeys function lines endobject beginanotherobjects ... 

you can use tell , seek move inside file, want do, use this, hacked together:

import re  # so, we're looking object 'heythere' objectname = 'heythere'  open('input.txt', 'r+') f:     line = f.readline()     pos = f.tell()     found = false     while line:          # want alter part          # right objectalias, use 'found' flag         if 'objectalias ' + objectname in line:             found = true         if 'endobject' in line:             found = false          if found , 'beginkeyframe' in line:              # found keyframe part, read lines              # until endkeyframe , count each line 'default'             sub_line = f.readline()             frames = 0             while not 'endkeyframe' in sub_line:                 if 'default' in sub_line:                     frames += 1                 sub_line = f.readline()              # since want override 'beginkeyframe',             # have move in file before line             f.seek(pos)              # read rest of file, skip             # old 'beginkeyframe' line want replace             f.readline()             rest = f.read()              # jump right position again             f.seek(pos)              # , write our new 'beginkeyframe' line             f.write(re.sub('\d+', str(frames), line, count=1))              # , write rest of file             f.write(rest)             f.truncate()             # nothing here anymore, quit loop             break          # before reading new line, keep track         # of our current position in file         pos = f.tell()         line = f.readline() 

the comments pretty explain what's going on.

given input file like

foo bar beginobject something objectalias notme lines more lines beginkeyframe 22 12    foo bar default foo default bar default endkeyframe endobject foo bar beginobject something objectalias heythere lines more lines beginkeyframe 43243 12    foo bar default foo default bar default foo default bar default foo default bar endkeyframe endobject 

it replace line

beginkeyframe 43243 12    

with

beginkeyframe 6 12    

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -