Python:跳过'for'循环迭代
Python: Skip 'for' loop interation
我找不到与此特定问题相匹配的问题;我正在 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.")
我发现了问题:
片段:
if current_index+1 < len(file_content):
if file_char == '/' and file_content[current_index+1] == '*':
multi_line_comment = True
列表太靠前了一个标签。
我真的不期待学习 Python 的其余部分。
我找不到与此特定问题相匹配的问题;我正在 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.")
我发现了问题:
片段:
if current_index+1 < len(file_content):
if file_char == '/' and file_content[current_index+1] == '*':
multi_line_comment = True
列表太靠前了一个标签。
我真的不期待学习 Python 的其余部分。