如何在for循环的txt文件中打印输出?

时间:2017-10-24 04:26:38

标签: python python-2.7

def input_sentence():
    sppc = BetterICP(grammar2)
    with open("output.txt","w") as op:
        with open("input.txt", "r") as ins:
            array = []
            temp = []
            for line in ins:
                    array.append(line)
            for a in array:     
                op.write(str(sppc.parse(a.split()))

我需要编写我的输出,我将从str(sppc.parse(a.split())获取但不能将其写入文件

2 个答案:

答案 0 :(得分:0)

你在op.write中错过了一个')'(str(sppc.parse(a.split()))

答案 1 :(得分:0)

似乎sppc.parse返回一个可迭代对象。尝试使用:

for a in array:
    for b in sppc.parse(a.split())
        op.write(str(b))