我无法访问内部if else语句中的变量

2024-06-28 11:03:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个项目。我有C++和java背景,但是我没有Python背景。 我给了txt文件如下:

G1 X000 Y00
X00
Z000
G0 X121.212 Z5.32
X0.00 Y3.000

我需要加G。。直到它看到新的G。可以是G1、G0或G5等。。所以结果如下

G1 X000 Y00
G1 X00
G1 Z000
G0 X121.212 Z5.32
G0 X0.00 Y3.000

我的代码在下面

command = ""
with open('Model.ngc', 'r') as f:
    with open("out.txt", "w") as f1:
        for line in f:
            if line in ['\n', '\r\n']:
                f1.write(line)
            elif line.startswith('M', 0, 1):
                f1.write(line)
            else:
                command = line.split(' ', 1)[0]
                if command == 'G0' or command == 'G00':
                    command = 'G00'
                elif command == 'G1' or command == 'G01':
                    command = 'G01'
                elif command == 'G2' or command == 'G02':
                    command = 'G02'
                elif command == 'G3' or command == 'G03':
                    command = 'G03'

                if (line.startswith('G0 ', 0, 3) or line.startswith('G1 ', 0, 3) or line.startswith('G2 ', 0, 3) or
                    line.startswith('G3 ', 0, 3) or line.startswith('G4 ', 0, 3) or line.startswith('G5 ', 0, 3) or
                    line.startswith('G6 ', 0, 3) or line.startswith('G7 ', 0, 3) or line.startswith('G8 ', 0, 3) or
                    line.startswith('G9 ', 0, 3)):

                    if command == 'G00':
                        f1.write(line.replace('G0', 'G00'))
                    elif command == 'G01':
                        f1.write(line.replace('G1', 'G01'))
                    elif command == 'G02':
                        f1.write(line.replace('G2', 'G02'))
                    elif command == 'G03':
                        f1.write(line.replace('G3', 'G03'))
                    elif command == 'G04':
                        f1.write(line.replace('G4', 'G04'))
                    elif command == 'G05':
                        f1.write(line.replace('G5', 'G05'))
                    elif command == 'G06':
                        f1.write(line.replace('G6', 'G06'))
                    print(command)
                else:
                    # print(line)

                    if ( line.startswith('G00') or line.startswith('G01') or line.startswith('G02') or
                         line.startswith('G03') or line.startswith('G04') or line.startswith('G05') or
                         line.startswith('G06') or line.startswith('G07') or line.startswith('G08') or line.startswith('G09')):
                        # Because it can be already converted
                        f1.write(line)
                    else:
                        f1.write(command + line)

我正试图从上一行获取G1或G0,并将其分配给命令。它起作用了。当我试图在最后一个else语句中到达时,它显示我的命令 变量是空的,我不知道如何处理它


Tags: oriflinecommandreplacewritef1elif
2条回答

您可以在字符串中检查'G',将其子字符串化,并将其用于没有子字符串的行。 我试着用我的方式

with open('newfile.txt','r') as f:
Gval = 'G0'
newlines=[]
for line in f:
    if 'G' in line:
        Gval = line[line.index('G'):line.index(' ')]
        newlines.append(line)
    else:
        l = Gval+' '+line
        newlines.append(l)
f.close()
newf = open('filenew.txt','w')
for item in newlines:
    newf.write("%s"%item)   
newf.close()

把每行分开,检查第一个单词是否以G开头。如果是这样的话,那么就把它作为前缀添加pre。 如果该行没有前缀,则将存储的单词与带空格的行连接起来

pre=""
for  line  in f1.read().splitlines():
    word = line.split()
    if word and word[0] and word[0][0]=="G":
        pre=word[0]
    else:
        line = " ".join([pre, line])
    print line

输出

G1 X000 Y00
G1 X00
G1 Z000
G0 X121.212 Z5.32
G0 X0.00 Y3.000

希望有帮助

相关问题 更多 >