Python:跳过'for'循环迭代

时间:2015-03-12 02:54:01

标签: python loops for-loop iteration skip

我找不到符合此特定问题的问题;我正在用Python编写一个自动代码缩减器,但我似乎无法找到如何跳过'for'循环的当前迭代。

你能帮忙吗?

这是脚本:

import os
import sys
try:
    file_path = sys.argv[1]
except IndexError:
    print("No file given")
    sys.exit()

minified_file = ""
dbl_quotes = False
sgl_quotes = False
line_comment = False
multi_line_comment = False
current_index = 0

if os.path.isfile(file_path):
    print('Path: ' + file_path)
    char_check = ""
    file_handle = open(file_path)
    file_content = file_handle.read()
    for file_char in file_content:
        if sgl_quotes or dbl_quotes:
            if file_char == "'" and not dbl_quotes:
                sgl_quotes = False
            if file_char == '"' and not sgl_quotes:
                dbl_quotes = False
            minified_file += file_char
            continue
        else:
            if file_char == "'":
                sgl_quotes = True
                minified_file += file_char
                continue
            elif file_char == '"':
                dbl_quotes = True
                minified_file += file_char
                continue

            if current_index+1 < len(file_content):
                if file_char == '/' and file_content[current_index+1] == '*':
                    multi_line_comment = True

    if current_index-1 >= 0:
        if multi_line_comment == True and file_char == '/' and file_content[current_index-1] == '*':
            multi_line_comment = False
            continue

    if current_index+1 < len(file_content):
        if file_char == '/' and file_content[current_index+1] == '/' and not line_comment:
            line_comment = True
            continue

    if line_comment:
        if file_char == '\r' or file_char == '\n':
            line_comment = False
            continue

    if line_comment or multi_line_comment:
        continue

    if file_char != '\r' and file_char != '\n' and file_char != '\t':
        minified_file += file_char

    current_index += 1

print(minified_file)
wait = input("PRESS ENTER TO CONTINUE.")

1 个答案:

答案 0 :(得分:0)

我发现了这个问题:

摘录:

if current_index+1 < len(file_content):
    if file_char == '/' and file_content[current_index+1] == '*':
        multi_line_comment = True

将一个标签列表太远了。

我真的不期待学习Python的其余部分。