English 中文(简体)
Python Python Python Python
原标题:Looping in Python and keeping current line after sub routine
  • 时间:2011-06-29 04:52:00
  •  标签:
  • python

I ve been trying to nut out an issue when looping in python 3. When returning from sub routine the "line" variable has not incremented.

How do I get the script to return the latest readline from the subsroutine?

简称

def getData(line):
      #print(line)
      #while line in sTSDP_data:
      while "/service/content/test" not in line:
            line = sTSDP_data.readline()

import os, sys

sFileTSDP = "d:/ess/redo/Test.log"
sTSDP_data = open(sFileTSDP, "r")

for line in sTSDP_data:
      if "MOBITV" in line:
            getData(line)   #call sub routine
            print(line)

I m stepping through a large file and on a certain string I need to call a sub routine to process the next 5 (or 100) lines of data. When the sub routine completes and returns to the main program, it would be better to have it continue on from the last readline in the sub routine, not the last readline in the main program.

Daan s answer did the trick.

问题回答

如何使用返回声明?

def getData(line):
      #print(line)
      #while line in sTSDP_data:
      while "/service/content/test" not in line:
            line = sTSDP_data.readline()
      return line

import os, sys

sFileTSDP = "d:/ess/redo/Test.log"
sTSDP_data = open(sFileTSDP, "r")

for line in sTSDP_data:
      if "MOBITV" in line:
            line = getData(line)   #call sub routine
            print(line)

了解您变量的范围。 您的职责范围与你的工作路线不同。

转让也没有参考。 这意味着,如果你在一项职能中重新分配一个变量,那么它就会在另一项职能中赢得一个变数(除非有具体例外,例如<条码>:全球和<条码>非当地)。 (请注意:这是再分配的,而不是修改的。) 如果您修改一份清单,所有提及名单的条目都是“修改的”。

简略位置return line at the end of AccessData(line)

def getData(line):
      #print(line)
      #while line in sTSDP_data:
      while "/service/content/test" not in line:
            line = sTSDP_data.readline()
      return line




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签