如果next(item)移动到列表中的下一个项目,则相当于next next(item)python

时间:2015-01-18 05:43:30

标签: python file next

见下文的代码。

    def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in  elif statements, executes code in if statment. Otherwise, ignores line    

    with open(file,'r') as f:
        for line in f:  #starts for loop for all if statements
            line = line.strip()
            if line.isdigit():
                start = int(line)
                score.initialScore(start)
                print(line)#DEBUG TEST**** #checks if first line is a number if it is adds it to intial score

            elif len(line) == 0:
                print(line)#DEBUG TEST****
                continue        #if a line has nothing in it. skip it  

            elif line == 'o' or line == 'O':
                amount = next(f)
                print(line)#DEBUG TEST****
                score.updateOne(amount) #if line contains single score marker, Takes content in next line and
                                        #inserts it into updateOne
            elif line == 'm'or line == 'M':
                scoreList = next(f)
                lst = []
                for item in scoreList:
                    print(line)#DEBUG TEST****
                    lst.append(item)
                    score.updateMany(lst) # if line contains list score marker, creates scoreList variable and places the next line into  that variable
                                          # creates lst variable and sets it to an empty list
                                          # goes through the next line with the for loop and appends each item in the next line to the empty list
                                          # then inserts newly populated lst into updateMany

            elif line == 'X':
                print(line)#DEBUG TEST****
                score.get(self)
                score.average(self) # if line contains terminator marker. prints total score and the average of the scores.
                                    # because the file was opened with the 'with' method. the file closes after 

我正在尝试使用的文件看起来像这样:

50

0

30

0

40

中号

10 20 30

0

5

1 2 3

X

如果代码看到'O'或'o',那么它需要在代码中接下一行并将其添加到正在运行的分数中。但是下一行是一个空格...所以我需要跳到'O'或'o'之后的第二行。

我正在考虑为此做一个例外,但在我走这条路之前,我想知道是否有人可能知道更好的方法。

3 个答案:

答案 0 :(得分:1)

如果您想继续f跳过仅限空格的项目,

while True:
    x = next(f).strip()
    if x: break

将起作用,

for x in f:
    x = x.strip()
    if x: break

不同之处在于,如果f中的非全部空间项目后面有,该怎么办?前者将以StopIteration例外退出,后者退出for循环,但x设置为''除外{{1}}。选择你的毒药(你愿意处理的退出形式)并相应地编码!

答案 1 :(得分:0)

如下:

For line in lines:
  if type(line) == 'int':
     oneCount += line
  elif type(line) == 'list':
     manyCount.append(line)
  elif type(line) == 'str' and line != 'x':
     continue
  elif type(line) == None:
     continue
  else:
     print scores

答案 2 :(得分:0)

考虑这个问题的有用模型是状态机。

代码有3种状态:

  1. 读取命令代码。
  2. 添加单个分数(在" O")。
  3. 添加多个分数(在" M")。
  4. 通过将变量保持为当前状态,您可以在不跳过的情况下处理输入。

    现在,空行看起来没有用处,所以你可以从输入中删除它们,如下所示:

    ...
    non_empty_lines = (line for line in f if line.strip())
    for line in non_empty_lines:
      ... do your thing ...
    

    生成器表达式将过滤所有空格的行。

    如果由于某种原因你不能使用生成器表达式,那么在循环中执行:

    ...
    for line in f:
      if not line.strip():
        continue
      ...