我不断收到这种“写一个封闭的文件错误”,同时试图编译下面的代码:蟒蛇写入文件
fout = open('markov_output.txt', 'w')
for i in range(MAXGEN) :
# get our hands on the list
key = (w1,w2)
sufList = table[key]
# choose a suffix from the list
suf = random.choice(sufList)
if suf == NONWORD : # caught our "end story" marker. Get out
if len(line) > 0 :
fout.write(line)
break
if len(line) + len(suf) > MAX_LINE_LEN :
fout.write(line)
line = ""
line = line + " " + suf
w1, w2 = w2, suf
fout.close()
你为什么要关闭**循环内的文件**?这可能只会写入一条记录,然后该文件将被关闭。这是你的意图吗?还是你的缩进错了? – 2010-12-06 18:23:39