Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我无法访问内部if-else语句中的变量_Python - Fatal编程技术网

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

Python 我无法访问内部if-else语句中的变量,python,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.n

我正在做一个项目。我有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语句中尝试到达时,它显示我的命令
变量为空,我不知道如何处理它

拆分每行,检查第一个单词是否以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

希望有帮助

您可以检查字符串中的'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()

它不起作用。回溯(最后一次调用):文件“NgcToGcode.py”,第6行,在if-word[0]和word[0][0]=“G”:indexer:list index超出范围您能检查读取的行是否正确吗?因为,我在控制台中尝试过,直到那时我才在这里分享了答案!此代码段应该紧跟在
之后,open(“out.txt”,“w”)为f1:
,否则,当读取的行为空时,会出现这种情况!我已经为它添加了编辑。