Python中使用';与';陈述

Python中使用';与';陈述,python,Python,我将输入和输出变量分配给函数定义,以从GUI中获取条目,输入(读取)一个.txt文件并创建(写入)一个输出.txt文件,该文件将拆分以下特定数据列: def runTransferDialog(): e1, e2 = StringVar(), StringVar() PackDialog(e1, e2) input, output = e1.get(), e2.get() # GUI entries assigned to variables if

我将输入和输出变量分配给函数定义,以从GUI中获取条目,输入(读取)一个.txt文件并创建(写入)一个输出.txt文件,该文件将拆分以下特定数据列:

def runTransferDialog():
    e1, e2 = StringVar(), StringVar()
    PackDialog(e1, e2)
    input, output = e1.get(), e2.get()         # GUI entries assigned to variables
    if input !='' and output !='':
        with open(input, 'r') as input1:        # read input .txt file
            with open(output, 'w') as output1:   # write input .txt file
                for line in input1:
                    columns = line.strip().split()
                    output1.write('{:8}{:8}\n'.format(columns[0], columns[3])

编译后,我得到“IndexError:list index out-range”,确实生成了一个output.txt文件,但其中没有数据列。发生了什么事?

很可能
列的
元素少于4个,因此最后一行中的
列[3]
正在提升
索引器。不知道什么是
,就很难说。将最后一行设置为获取一些调试信息:

try:
    output1.write('{:8}{:8}\n'.format(columns[0], columns[3])
except IndexError, e:
    print repr(line)
    # Alternatively
    #output1.write("Error: " + repr(line))
    raise

类似的常见错误是文件以“\n”结尾
查找空的最后一行。

请不要将
input
用作变量名。它隐藏了内置函数。