变量的值不会在嵌套循环中递增(用 Python 写入文件)
Variable's value doesn't increment within nested loops (File writing with Python)
我有一个代码可以解析多个文本文件并从每个文件中提取给定的部分并将它们写入另一个文本文件。我一遍又一遍地重复这个过程,提取文件的下一部分并将它们写入另一个文本文件。
我使用变量 cube_i
从一个文件移动到另一个文件,使用变量 layer
从文本文件的一部分移动到另一部分。但是,我创建的循环不会超过 cube_i=0
和 layer=0
。关于如何继续增加这些变量的值有什么建议吗?
rgx_start = re.compile(r'Z down') # Second line before the start of the layer code: "G0Z2 ; Z down"
rgx_end = re.compile(r'Layer #') # Second line after the end of the layer code: "; Layer #<integer>"
if layer in range(0, (layers_total + 1)):
final_code.write('; ------------------------------\n')
label = '; Layer #{}\n'.format(layer)
final_code.write(label)
final_code.write('; ------------------------------\n')
with open(first_file,'r') as gcode: # Opening the first file to write the powder drop
for i, line in enumerate(gcode.readlines()):
if i in range(powdrop_start, (powdrop_end + 1)): # Writing the powder drop
final_code.write(line)
cube_i = 0 # Reseting the cube_i variable
if cube_i in range(0, (parts_total + 1)):
label_cube = '; PART #{}\n'.format(cube_i)
final_code.write(label_cube)
# Opening each text file separately
with open(f_path, 'r') as gcode: # f_path contains cube_i
lines = gcode.readlines()
start_i = prev_start[cube_i]
for i, line in enumerate(lines[start_i + 1:]): # Searching for the beginning of given layer
if rgx_start.search(line):
layer_start = start_i + i + 3
break
for i, line in enumerate(lines[layer_start + 1:]): # Searching for the end of given layer
if rgx_end.search(line):
layer_end = layer_start + i - 1
break
for i, line in enumerate(lines[layer_start:layer_end + 1]):
final_code.write(line)
prev_start.append(layer_start)
prev_end.append(layer_end)
cube_i = + 1
if layer == 0: # Writing the zeroth layer footer
with open(first_file, 'r') as gcode:
for i, line in enumerate(gcode.readlines()):
if i == zeroth_ftr:
final_code.write(line)
layer =+ 1
代码中有几个 for 循环被错误地写成了 if 语句。
截断的正确代码是:
rgx_start = re.compile(r'Z down') # Second line before the start of the layer code: "G0Z2 ; Z down"
rgx_end = re.compile(r'Layer #') # Second line after the end of the layer code: "; Layer #<integer>"
for layer in range(0, (layers_total + 1)):
final_code.write('; ------------------------------\n')
label = '; Layer #{}\n'.format(layer)
final_code.write(label)
final_code.write('; ------------------------------\n')
with open(first_file,'r') as gcode: # Opening the first file to write the powder drop
for i, line in enumerate(gcode.readlines()):
if i in range(powdrop_start, (powdrop_end + 1)): # Writing the powder drop
final_code.write(line)
cube_i = 0 # Reseting the cube_i variable
for cube_i in range(0, (parts_total + 1)):
label_cube = '; PART #{}\n'.format(cube_i)
final_code.write(label_cube)
# Opening each text file separately
with open(f_path, 'r') as gcode: # f_path contains cube_i
lines = gcode.readlines()
start_i = prev_start[cube_i]
for i, line in enumerate(lines[start_i + 1:]): # Searching for the beginning of given layer
if rgx_start.search(line):
layer_start = start_i + i + 3
break
for i, line in enumerate(lines[layer_start + 1:]): # Searching for the end of given layer
if rgx_end.search(line):
layer_end = layer_start + i - 1
break
for i, line in enumerate(lines[layer_start:layer_end + 1]):
final_code.write(line)
prev_start.append(layer_start)
prev_end.append(layer_end)
cube_i = + 1
if layer == 0: # Writing the zeroth layer footer
with open(first_file, 'r') as gcode:
for i, line in enumerate(gcode.readlines()):
if i == zeroth_ftr:
final_code.write(line)
layer =+ 1
这应该适用于您的情况。
我有一个代码可以解析多个文本文件并从每个文件中提取给定的部分并将它们写入另一个文本文件。我一遍又一遍地重复这个过程,提取文件的下一部分并将它们写入另一个文本文件。
我使用变量 cube_i
从一个文件移动到另一个文件,使用变量 layer
从文本文件的一部分移动到另一部分。但是,我创建的循环不会超过 cube_i=0
和 layer=0
。关于如何继续增加这些变量的值有什么建议吗?
rgx_start = re.compile(r'Z down') # Second line before the start of the layer code: "G0Z2 ; Z down"
rgx_end = re.compile(r'Layer #') # Second line after the end of the layer code: "; Layer #<integer>"
if layer in range(0, (layers_total + 1)):
final_code.write('; ------------------------------\n')
label = '; Layer #{}\n'.format(layer)
final_code.write(label)
final_code.write('; ------------------------------\n')
with open(first_file,'r') as gcode: # Opening the first file to write the powder drop
for i, line in enumerate(gcode.readlines()):
if i in range(powdrop_start, (powdrop_end + 1)): # Writing the powder drop
final_code.write(line)
cube_i = 0 # Reseting the cube_i variable
if cube_i in range(0, (parts_total + 1)):
label_cube = '; PART #{}\n'.format(cube_i)
final_code.write(label_cube)
# Opening each text file separately
with open(f_path, 'r') as gcode: # f_path contains cube_i
lines = gcode.readlines()
start_i = prev_start[cube_i]
for i, line in enumerate(lines[start_i + 1:]): # Searching for the beginning of given layer
if rgx_start.search(line):
layer_start = start_i + i + 3
break
for i, line in enumerate(lines[layer_start + 1:]): # Searching for the end of given layer
if rgx_end.search(line):
layer_end = layer_start + i - 1
break
for i, line in enumerate(lines[layer_start:layer_end + 1]):
final_code.write(line)
prev_start.append(layer_start)
prev_end.append(layer_end)
cube_i = + 1
if layer == 0: # Writing the zeroth layer footer
with open(first_file, 'r') as gcode:
for i, line in enumerate(gcode.readlines()):
if i == zeroth_ftr:
final_code.write(line)
layer =+ 1
代码中有几个 for 循环被错误地写成了 if 语句。 截断的正确代码是:
rgx_start = re.compile(r'Z down') # Second line before the start of the layer code: "G0Z2 ; Z down"
rgx_end = re.compile(r'Layer #') # Second line after the end of the layer code: "; Layer #<integer>"
for layer in range(0, (layers_total + 1)):
final_code.write('; ------------------------------\n')
label = '; Layer #{}\n'.format(layer)
final_code.write(label)
final_code.write('; ------------------------------\n')
with open(first_file,'r') as gcode: # Opening the first file to write the powder drop
for i, line in enumerate(gcode.readlines()):
if i in range(powdrop_start, (powdrop_end + 1)): # Writing the powder drop
final_code.write(line)
cube_i = 0 # Reseting the cube_i variable
for cube_i in range(0, (parts_total + 1)):
label_cube = '; PART #{}\n'.format(cube_i)
final_code.write(label_cube)
# Opening each text file separately
with open(f_path, 'r') as gcode: # f_path contains cube_i
lines = gcode.readlines()
start_i = prev_start[cube_i]
for i, line in enumerate(lines[start_i + 1:]): # Searching for the beginning of given layer
if rgx_start.search(line):
layer_start = start_i + i + 3
break
for i, line in enumerate(lines[layer_start + 1:]): # Searching for the end of given layer
if rgx_end.search(line):
layer_end = layer_start + i - 1
break
for i, line in enumerate(lines[layer_start:layer_end + 1]):
final_code.write(line)
prev_start.append(layer_start)
prev_end.append(layer_end)
cube_i = + 1
if layer == 0: # Writing the zeroth layer footer
with open(first_file, 'r') as gcode:
for i, line in enumerate(gcode.readlines()):
if i == zeroth_ftr:
final_code.write(line)
layer =+ 1
这应该适用于您的情况。