我在使用 python 将字符串写入文件时遇到问题:(我正在尝试使用 python 生成一些 C 程序)我拥有的代码如下:
filename = "test.txt"
i = 0
string = "image"
tempstr = ""
average1 = "average"
average2 = "average*average"
output = ""
FILE = open(filename,"w")
while i < 20:
j = 0
output = "square_sum = square_sum + "
while j < 20:
tempstr = string + "_" + str(i) + "_" + str(j)
output = output + tempstr + "*" + tempstr + " + " + average2 + " - 2*" + average1 + "*" + tempstr
if j != 19:
output = output + " + "
if j == 19:
output = output + ";"
j = j + 1
output = output + "\n"
i = i + 1
print(output)
FILE.writelines(output)
FILE.close
打印给了我正确的输出,但是文件缺少最后一行,并且缺少一些倒数第二行。将字符串写入文件有什么问题?
谢谢!